数据表没有排序没有错误

时间:2012-12-07 11:55:50

标签: c# datatable

我正在使用下面的代码对程序运行的数据进行排序,但是行中的数据没有被排序......并且无法找到原因。

DataTable extractedData = new DataTable();
//data added to datatable extractedData
DataView dv = new DataView();
dv =extractedData.AsDataView();
dv.Sort = dv.Table.Columns[10].ColumnName + "  DESC";
extractedData = dv.Table;

以下代码使用apose.slides将代码添加到演示文稿表中。

 string val = "";
 int col = 0;
 int row = 0;
 for (int i = 1; i < table.Rows.Count; i++)
 {
    for (int j = 0; j < table.Columns.Count; j++)
    {
        if (j == 0)
         {
             val = extractedData.Rows[row][valuesUsed[col]].ToString();
             string[] removeQuestion = val.Split('[', ']');
             val = removeQuestion[1];
         }
         else
         {
             val = extractedData.Rows[row][valuesUsed[col]].ToString();
             if (val == "-" || val == "")
             {
                 val = extractedData.Rows[row][valuesUsed[col]].ToString();
             }
             else
             {
                 val = Convert.ToString(Math.Round(Convert.ToDouble(extractedData.Rows[row][valuesUsed[col]]), MidpointRounding.AwayFromZero));
             }
         }
         table[j, i].TextFrame.Text = val;
         col++;
     }
     row++;
     col = 0;
 }

2 个答案:

答案 0 :(得分:1)

这将有效: -

DataView dv = extractedData.DefaultView;
dv.Sort = extractedData.Columns[10].ColumnName + "  DESC";

您需要使DataView成为DataTables的默认视图,以便它能够正常工作。

这是您应该查看的链接。

http://msdn.microsoft.com/en-us/library/system.data.dataview.sort.aspx

答案 1 :(得分:0)

将代码的最后一行更改为dv.toTable()而不是dv.Table

DataTable extractedData = new DataTable();
//data added to datatable extractedData
DataView dv = new DataView();
dv = extractedData.DefaultView;
dv.Sort = "ColumnName  DESC";   //mention exact name of your column
//or else
//dv.Sort = extractedData.Columns[9].ColumnName + "  DESC";
extractedData = dv.toTable();  //Instead of dv.Table;

有关信息请参阅此link