Jim Brad
Jim Brad

Reputation: 393

datatable not sorting no errors

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;
 }

Upvotes: 1

Views: 452

Answers (2)

Mr_Green
Mr_Green

Reputation: 41832

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.

Upvotes: 0

Derek
Derek

Reputation: 8628

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

Upvotes: 1

Related Questions