Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DataView排序不起作用_C#_Sorting_Dataview - Fatal编程技术网

C# DataView排序不起作用

C# DataView排序不起作用,c#,sorting,dataview,C#,Sorting,Dataview,我正在尝试用C对数据视图进行排序,但它对列ArtNr不起作用。。我还尝试对Menge、Einzelpreis和Preis列进行排序,结果成功了。但我无法对第一列进行排序。列中只有int值 这是我的代码: foreach (DataGridViewRow row in dataGridView2.Rows) { DataRow row1; row1 = dtSource.NewRow();

我正在尝试用C对数据视图进行排序,但它对列ArtNr不起作用。。我还尝试对Menge、Einzelpreis和Preis列进行排序,结果成功了。但我无法对第一列进行排序。列中只有int值

这是我的代码:

foreach (DataGridViewRow row in dataGridView2.Rows)
            {
                DataRow row1;

                row1 = dtSource.NewRow();
                row1["ArtNr."] = row.Cells[2].Value.ToString();
                row1["Bezeichung"] = row.Cells[3].Value.ToString();
                row1["Menge"] = row.Cells[1].Value.ToString();
                row1["Einzelpreis"] = row.Cells[4].Value.ToString();
                row1["Preis"] = (Convert.ToDecimal(row.Cells[1].Value) * Convert.ToDecimal(row.Cells[4].Value)).ToString();

                dtSource.Rows.Add(row1);
            }

            DataView dv = dtSource.DefaultView;
            //dv.Sort = "ArtNr. DESC"; -> does not work
            dv.Sort = dv.Table.Columns[0].ColumnName + " ASC"; //does not work

            DataTable sortedDT = dv.ToTable();

            return sortedDT;
你可以试试这个

DataView dv = new DataView(dtSource, "", "SortColumn1,SortColumn2...", DataViewRowState.CurrentRows);

由于需要特定列进行排序,请尝试以下操作: DataView dv=dtSource.FirstOrDefault; 然后,如果要检索所有数据,可以将dv设置为如下列表:
DataView dv=dtSource.ToList

这些列仅为int值。不,它们不是:row.Cells[2].Value.ToString;它们看起来像绳子。Nr之后的这段时间看起来很麻烦。您的数据行中有数据吗?为了让它正常工作。应该在sql端用方括号[]括起来。因为列名中的句点是坏时间的单程票,datagrid元素的名称周围是否仍有方括号?