Question : Problem with Sort in Dataview

Hi Experts, I am not really sure why this is a problem, it hasn't been in the past. But I am creating a DataView and trying to sort the contents. The sort dos not seem to be working, the results are not be sorted as expected.
I will attach some code and would appreciate your help.

As you look at the code there is a column called ItemType which is either equal to"account" or "pos", they are string values and I need the results of the dataview to be sorted with the account values first.
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
if (Session["DataTable"] != null)
                table = (DataTable)Session["DataTable"];
 
            // SORT DATATABLE AND MAKE SURE ACCOUNTS ARE LISTED FIRST
            string sort = "ItemType Desc";
            DataView dv = new DataView(table, "", sort, DataViewRowState.CurrentRows);
 
            InsertInvoiceValues(dv);

Answer : Problem with Sort in Dataview

Hai,

U have sorted the rows in the defaultview. but in the for loop u r accessing again the unsorted rows of the datatable instead of the sorted rows of the view. That is the problem.

instead of this for loop,
foreach (DataRow dr in dv.Table.Rows)
            {

just use,
System.Data.DataRowView dr;
for(int rowIndex=0; rowIndex < dv.Count ; rowIndex++)
{
  dr = dv[rowIndex];
  //.. then you can access the values asusual using dr["Column Name"]
MemberID = Convert.ToInt32(dr["MemberID"]);
..etc


Regards
Vinodh
Random Solutions  
 
programming4us programming4us