为什么当我从数据表导出到csv时,它按我想要的另一列进行相同的排序

本文关键字:一列 排序 我想要 数据表 csv 为什么 | 更新日期: 2023-09-27 17:50:13

我想将我的DataTable导出为CSV。DataTable按列"Czas"排序,但当我导出它时,输出文件中的数据按"Nrkarty"排序。

这里有我的代码片段:

time1 = dateTimePicker1.Text.ToString() + dateTimePicker3.Text.ToString()+":00";
time2 = dateTimePicker2.Text.ToString() + dateTimePicker4.Text.ToString()+":59";
DataTable DT3 = new DataTable();
{
    using (SQLiteTransaction transaction = sql_con.BeginTransaction())
    {
        using (SQLiteCommand command = sql_con.CreateCommand())
        {
            command.CommandText = "SELECT IDX, Nrkarty, Czas FROM Zdarzenia WHERE datetime(Czas) BETWEEN datetime('" + time1 + "') AND datetime('" + time2 + "') ORDER BY Nrkarty";
            DA = new SQLiteDataAdapter(command);
            DA.AcceptChangesDuringFill = false;
            DT3.Clear();
            DT3.Columns.Clear();
            DA.Fill(DT3);
            DT3.Columns.Add("Kierunek", typeof(String));
            DT3.Columns.Add("Status", typeof(int));
            DT3.Columns.Add("Roznica", typeof(TimeSpan));
            if (DT3.Rows.Count != 0)
            {
                DateTime tempczas = DateTime.Parse(DT3.Rows[0]["Czas"].ToString());
                long temp = tempczas.Ticks;
                long suma = 0;
                long indeks = 0;
                foreach (DataRow temprow in DT3.Rows)
                {
                    DateTime czas = DateTime.Parse(temprow["Czas"].ToString());
                    long Ticks = czas.Ticks;
                    long roznica = Ticks - temp;
                    if (roznica < 0) { roznica = 0; }
                    long stala10m = new TimeSpan(0, 10, 0).Ticks;
                    long stala13h=new TimeSpan(17,0,0).Ticks;//zmiana zakladalem ze trwa 12 dlatego zrobilem 13 okazalo sie ze trwa 16 zrobilem 17
                    if (roznica < stala10m)
                    {
                        temprow["Roznica"] = roznica;
                        temprow["Kierunek"] = "X";//filtracja ktos nie wiedzial czy przylozyl
                        if (roznica == 0)//tylko dla pierwszej lini wynikow sortowania roznica =0;
                            indeks = 1;//domyslnie zakladam ze pierwszy wynik to przyjscie do pracy
                    }
                    else if (roznica < stala13h)
                    {
                        if (indeks == 0)
                        {
                            temprow["Roznica"] = new TimeSpan(0);
                            temprow["Kierunek"] = "WE";//jezeli parzyste przylozenie to to przylozenie musi byc rozpoczeciem pracy
                            indeks = 1;
                        }
                        else
                        {
                            temprow["Roznica"] = roznica;
                            suma += roznica;
                            temprow["Kierunek"] = "WY";//jezeli nieparzyste przylozenie to to przylozenie musi byc zakonczenie pracy
                            indeks = 0;
                        }
                    }
                    else
                    {
                        indeks = 1;
                        temprow["Roznica"] = new TimeSpan(0);
                        temprow["Kierunek"] = "WE";//jezeli minelo 17h od poprzedniego przylozenia to to przylozenie musi byc rozpoczeciem pracy
                    }
                    temp = Ticks;
                    temprow["Status"] = 0;
                }
                DT3.DefaultView.Sort = "Czas asc";
                dataGridView1.DataSource = DT3;
                DT4 = DT3;
                TimeSpan ssuma = new TimeSpan(suma);
                UInt32 czas_pracy = Convert.ToUInt32(ssuma.TotalMinutes);
                UInt32 czas_h = czas_pracy / 60;
                UInt32 czas_m = czas_pracy % 60;
                textBox1.Text = czas_h.ToString();
                textBox2.Text = czas_m.ToString();
                try
                {
                    dataGridView1.Columns["Czas"].DefaultCellStyle.Format = "yyyy-MM-dd HH:mm:ss";
                    dataGridView1.Columns["IDX"].Visible = true;
                    dataGridView1.Columns["Nrkarty"].Visible = true;
                    dataGridView1.Columns["Czas"].Width = 110;
                    dataGridView1.Columns["Nazwisko"].Width = 120;
                    dataGridView1.Columns["Imie1"].Width = 120;
                    //dataGridView1.Columns["Roznica"].DefaultCellStyle.Format = "HH:mm:ss";
                }
                catch (NullReferenceException)
                {
                    // This happens when settings values are empty
                }
            }
        }
        transaction.Commit();
    }
}

并导出为CSV:

public static void ExportToSpreadsheet(System.Data.DataTable dt, Stream myStream, string CB1)
{
    StreamWriter sw = new StreamWriter(myStream);//, System.Text.Encoding.Unicode);
    int iColCount = dt.Columns.Count;
    if (CB1 != "Wszystko")
    {
        for (int i = 0; i < iColCount; i++)
        {
            if ((dt.Columns[i].ColumnName == "Czas") || (dt.Columns[i].ColumnName == "Nazwisko") || (dt.Columns[i].ColumnName == "Imie1") || (dt.Columns[i].ColumnName == "Imie2"))
            {
                if (dt.Columns[i].ColumnName == "Czas")
                {
                    sw.Write("Data;Czas");
                }
                else
                    sw.Write(dt.Columns[i]);
                if (i < iColCount - 1)
                {
                    sw.Write(";");
                }
            }

        }
        sw.Write(sw.NewLine);
        foreach (DataRow dr in dt.Rows)
        {
            for (int i = 0; i < iColCount; i++)
            {
                if ((dt.Columns[i].ColumnName == "Czas") || (dt.Columns[i].ColumnName == "Nazwisko") || (dt.Columns[i].ColumnName == "Imie1") || (dt.Columns[i].ColumnName == "Imie2"))
                {
                    if (!Convert.IsDBNull(dr[i]))
                    {
                        /*if (dt.Columns[i].ColumnName == "Czas")
                        {
                            string tmp = dr[i].ToString();
                            StringBuilder s = new StringBuilder(tmp);
                            char x1 = s[0];
                            char x2 = s[1];
                            char x3 = s[6];
                            char x4 = s[7];
                            s[0] = x3;
                            s[1] = x4;
                            s[6] = x1;
                            s[7] = x2;
                            sw.Write(s.ToString());
                        }
                        else*/
                        {
                            if (dt.Columns[i].ColumnName == "Czas")
                            {
                                string x = dr[i].ToString();
                                string y = x.Replace(' ', ';');
                                sw.Write(y);
                            }
                            else
                                sw.Write(dr[i].ToString());
                        }
                    }
                    if (i < iColCount - 1)
                    {
                        sw.Write(";");
                    }
                }
            }
            sw.Write(sw.NewLine);
        }
    }
    else 
    {
        {
            for (int i = 0; i < iColCount; i++)
            {
                if ((dt.Columns[i].ColumnName == "Nrkarty") || (dt.Columns[i].ColumnName == "Czas") || (dt.Columns[i].ColumnName == "Kierunek") || (dt.Columns[i].ColumnName == "Status"))
                {
                    if (dt.Columns[i].ColumnName == "Czas")
                    {
                        sw.Write("Data;Czas");
                    }
                    else
                        sw.Write(dt.Columns[i]);
                    if (i < iColCount - 1)
                    {
                        sw.Write(";");
                    }
                }

            }
            sw.Write(sw.NewLine);
            foreach (DataRow dr in dt.Rows)
            {
                for (int i = 0; i < iColCount; i++)
                {
                    if ((dt.Columns[i].ColumnName == "Nrkarty") || (dt.Columns[i].ColumnName == "Czas") || (dt.Columns[i].ColumnName == "Kierunek") || (dt.Columns[i].ColumnName == "Status"))
                    {
                        if (!Convert.IsDBNull(dr[i]))
                        {
                            /*if (dt.Columns[i].ColumnName == "Czas")
                            {
                                string tmp = dr[i].ToString();
                                StringBuilder s = new StringBuilder(tmp);
                                char x1 = s[0];
                                char x2 = s[1];
                                char x3 = s[6];
                                char x4 = s[7];
                                s[0] = x3;
                                s[1] = x4;
                                s[6] = x1;
                                s[7] = x2;
                                sw.Write(s.ToString());
                            }
                            else*/
                            {
                                if (dt.Columns[i].ColumnName == "Czas")
                                {
                                    string x = dr[i].ToString();
                                    //string x = ((DateTime)dr[i]).ToString("yyyy/MM/dd HH:mm:ss", CultureInfo.InvariantCulture);
                                    string y = x.Replace(' ', ';');
                                    sw.Write(y);
                                }
                                else
                                    sw.Write(dr[i].ToString());
                            }
                        }
                        if (i < iColCount - 1)
                        {
                            sw.Write(";");
                        }
                    }
                }
                sw.Write(sw.NewLine);
            }
        }
    }
    sw.Close();
}

为什么当我从数据表导出到csv时,它按我想要的另一列进行相同的排序

ok..我做到了。在将Datatable导入到dataGridView之前,我只将DefaultView分配给Datatable。

datatable = datatable.DefaultView.ToTable();