Jump to content
  • Advertisement
Sign in to follow this  
Strayfire

C# DataGrid Filtering and Sorting

This topic is 4770 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sorry, I haven't really had much experience with any of these classes up until now. Could you please provide an example?

Share this post


Link to post
Share on other sites
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 Here
dv.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();

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
There doesn't seem to be a DataBind() method. Is that just for web forms?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!