views:

929

answers:

1

I am using C# and .NET 3.5 and have a GridView that I am setting the dataSource programatically in the code-behind page. I have data in a DataTable and then depending on a column value (isValid boolean) of each Row, I create a new row using DataRowView.AddNew() method into 1 of 2 DataViews - dvValid or dvInvalid. I am NOT creating a new DataTable.NewRow to add to the DataView Table. Then I bind the GridView to the appropriate dataView.

There is a problem when I am sorting the GridView. I am having a problem with 1 row not being sorted correctly, all other rows are sorted fine. I debugged my code and found that the DataView.Count is 1 more than the DataView.Table.Rows.Count even though I am calling DataView.Table.AcceptChanges() method. This is strange since the dataTable should have all committed rows and therefore the counts should be the same.

So why are the 2 counts different? A DataView is a subset of the DataTable so should it not have equal or less rows than the DataTable. When I populate the DataView, should I first create the DataTables rather than creating the DataView directly? Right now, I am directly creating a DataRowView without a dDataTableRow, is this the correct approach?

Thanks for your help.

Code snippet : C#

...

//get the data as DataTable
members = GetMemberDataTable ();

//create views from a new DataTable with no rows
dvValidMembers = new DataView (CreateMembersDT("ValidMembers"));
dvInValidMembers = new DataView (CreateMembersDT("InvalidMembers"));

//iterate thru each row and put into appropriate DataView
foreach (DataRow memberRow in members.Rows)
            {
                if ((bool)memberRow["isValid"])
                    //Add to valid members Dview
                    member = dvValidMembers.AddNew();
                else
                    //add to InValid members Dview
                    member = dvInvalidMembers.AddNew();

                member["memberID"] = memberRow["memID"];

            } //foreach

    dvInvalidMembers.Table.AcceptChanges();
    dvValidMembers.Table.AcceptChanges();

 }

 private System.Data.DataTable CreateMembersDT ( string tableName)
    {
        System.Data.DataTable dtMembers = new System.Data.DataTable(tableName);

        dtMembers.Columns.Add(new DataColumn("memID", typeof(int)));

        return dtMembers;
    }
A: 

That 1 row that isn't sorting right, could that be the last row?

I think you are missing a DataView.EndEdit():

foreach (DataRow memberRow in members.Rows)
{
    DataView dv;
    if (...) 
        //Add to valid members Dview
        dv = dvValidMembers;
    else
        dv = dvInvalidMembers;

    member = dv.Addnew();
    member["memberID"] = memberRow["memID"];
    dv.EndEdit();
}

But I would also like to note that you could probably use 2 Views with a Filter on isValid and then you would only need to point them to the original members table.

Henk Holterman
Thanks Henk, it is the last row. Let me try your other suggestion as well.
Pritika