Technical Snippet of the Day – Sub-sorting on a DataGridView

At work we had an interesting case of using a DataGridView which needed to be sorted on one column, and then sub-sorted on another. I was a bit worried about how ugly it could be, as I had done a little reading about doing multiple column sorts. Turns out it was a piece of cake and took just a few minutes to get it working. Check out the second sample on this MSDN page (entitled “Custom Sorting Using the SortCompare Event”). Not so bad, eh? I can’t express how much I love DataGridViews.

Oh, and in case anyone is wondering how to set the focus on a particular row of a DataGridView, do something like the following:

dataGridView.Rows[rowid].Selected = true;
//Unselect any other rows here, if necessary
dataGridView.CurrentCell = dataGridView.Rows[rowid].Cells[0″];

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s