C# DataGrid Filtering and Sorting

Started by
4 comments, last by intrest86 18 years, 9 months ago
I'm trying to find the first occurance of a certain value in one column of information in my DataGrid and then set the scroll to that row. The grid can be both sorted and filtered by the user, which is causing a problem. The data table that I am retrieving from the data grid is the original unsorted and unfiltered table. How do I get a copy of what is actually visible in the DataGrid, not the original unmodifed DataTable?
Advertisement
Try using a DataView. It encapsulates your DataSet and then lets you play with the data as far as sorting and filtering goes, while the actual DataSet isn't touched. You can then bind to the DataView to get the post-processed data.

As far as getting a copy, just keep the DataView around. Whenever you enumerate through it you will see exactly what the DataGrid sees.
Turring Machines are better than C++ any day ^_~
Sorry, I haven't really had much experience with any of these classes up until now. Could you please provide an example?
Quote:Original post by Strayfire
Sorry, I haven't really had much experience with any of these classes up until now. Could you please provide an example?

I'm assuming you have already used the DataSets from your post. In that case, you really do almost nothing different. A very simply example would be if we got a list of first and last names from a database. We decide we want to sort the first names of everyone who's last name is Smith.
DataSet ds = GetMyData(); //Get the DataSet you want to work with (usually from a database)DataView dv = new DataView(ds.Tables(0)); //Create a DataView that is a 'view' of one of the tables in our DataSet.  We will use this to sort and filter.//Sort and Filter Heredv.Sort = "FirstName";dv.RowFilter = "LastName='Smith'";//Set the DataView to be the DataGrid's source for info, and then call DataBind()myDataGrid.DataSource = dv;myDataGrid.DataBind();
Turring Machines are better than C++ any day ^_~
There doesn't seem to be a DataBind() method. Is that just for web forms?
Quote:Original post by Anonymous Poster
There doesn't seem to be a DataBind() method. Is that just for web forms?

Ah, yes. DataBind is just for web forms. Sorry, I don't do nomral applications that often. In a standard form App you just have to set the datasource and it should handle it from there.
Turring Machines are better than C++ any day ^_~

This topic is closed to new replies.

Advertisement