Geeks With Blogs
Sean Rhone

So, this is the first Duh post and thought this would be a good one. I've been at this current place about 5 months now and for all the grids in the web app they like to use DataViews as the datasource, not sure why but since I'm the new guy I thought I would go along for now. After upgrading the ASP.NET 1.1 app to a 2.0/3.5 framework I talked the business people into adding pagination/sorting to the app. I replaced the old DataGrids with GridViews but kept the DataView as the binding source. Well I ran into a problem when it came to sorting records, for some reason the last record in the grid was always improperly sorted no matter what. For the life of me I could not figure out why until I noticed the RowState property of the DataRowView. Not sure why the light bulb went off but it got me to thinking and sure enough it was the key.

So, to see the problem create a class file with some public properties and a collection to hold a few of them. If you create a class named Agent and a collection called Agents you can paste the code below in and use it. Comment out the BeginEdit/EndEdit lines for the first run, then uncomment and run again.

  oAgents = new Agents();DataView dView = null;// Create a DataTable for use in creating a new DataViewDataTable dt = new DataTable("Agents");// Iterate over collection of Agents instead of hard coding the columns needed in our DataTableforeach (Agent oAgent in oAgents.AllAgents)foreach (PropertyInfo PropInfo in oAgent.GetType().GetProperties())// Add a new DataColumn to our DataTable using reflectionnew DataColumn(PropInfo.Name, oAgent.GetType().GetProperty(PropInfo.Name).PropertyType));break;// Create a new DataView using our DataTablenew DataView(dt);foreach (Agent oAgent in oAgents.AllAgents)// Iterate over collection of Agents and create a new DataRow for eachforeach (PropertyInfo PropInfo in oAgent.GetType().GetProperties())DataRowView row = dView.AddNew();Console.WriteLine("Before RowState {0}", row.Row.RowState.ToString());null);Console.WriteLine(" After RowState {0}", row.Row.RowState.ToString());Console.WriteLine("End RowState {0}", row.Row.RowState.ToString());


















dView =










row[PropInfo.Name] = oAgent.GetType().GetProperty(PropInfo.Name).GetValue(oAgent,







So, notice when you run the code without the Begin/End Edit the last records RowState is "Detached" but all the record prior to the last add "Added". Now run the code again and notice they are all now "Added". These two lines (BeginEdit/EndEdit) fixed my sorting issue. Because all the records were not the same RowState, the records that were "Detached" were not sorted.

I spent a couple hours on this bone-head error, so hopefully you wont.

Posted on Monday, September 8, 2008 8:04 PM | Back to top

Comments on this post: Duh moment of August

# re: Duh moment of August
Requesting Gravatar...
Thanks for this information. This is a useful idea in my project. - Mark Zokle
Left by Red Robins on Sep 20, 2016 5:11 AM

Your comment:
 (will show your gravatar)

Copyright © Sean Rhone | Powered by: