1

I am using the code below to sort my data the program runs with out any isses but the data in the rows is not being sorted.. and cant find a reason why this would be the case.

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;

This code below is using apose.slides to add code into a presentation table.

 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;
 }
Mr_Green
  • 40,727
  • 45
  • 159
  • 271
Jim Brad
  • 383
  • 1
  • 5
  • 12

2 Answers2

1

This will work :-

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

You were required to make the DataView the DataTables default view inorder for it to work.

Here's a link you should check out too.

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

Derek
  • 8,300
  • 12
  • 56
  • 88
  • No worries. You dont need that last line in your code @Mr_Green. – Derek Dec 07 '12 at 12:35
  • The OP is working with `extractedData` so adding the `DefaultView` to the `extractedData`.. isnt it correct? – Mr_Green Dec 07 '12 at 12:37
  • extractedData is the DataTable, your assigning the DataView as its Defaultview, therefore you don't need that last line of code. Try it. – Derek Dec 07 '12 at 12:38
  • extractedData = dv.toTable(); isnt required. – Derek Dec 07 '12 at 12:40
  • No, it is necessary.. If you are doing `extractedData.DefaultView.Sort = dataTable.Columns[9].ColumnName + " DESC";` in one line then it is not necessary.. – Mr_Green Dec 07 '12 at 12:46
0

Change the last line of your code to dv.toTable() instead of 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;

For information go through this link.

Community
  • 1
  • 1
Mr_Green
  • 40,727
  • 45
  • 159
  • 271