views:

13

answers:

1

Hi,

I have some problem with DataGridView in C#.

case is:

I do some update on database then I reload DataGridView with new values:

myDataGridView.DataSource = myDataSet.Tables[0]

Everything is ok, but recently I was asked if there is possibility to keep the same column filtering applied after reloading data?

What would be approach to do this filtering case?

Thanks for any ideas.

A: 

Ok, I found the solution, maybe it will help somebody: [btw. I made some language mistake filtering = sorting ;-)]

   DataGridViewColumn oldColumn = dataGridView1.SortedColumn;

   ListSortDirection direction;
   if (dataGridView1.SortOrder == SortOrder.Ascending) direction = ListSortDirection.Ascending;
   else direction = ListSortDirection.Descending;

   databaseUpdateFunction();

   DataGridViewColumn newColumn = dataGridView1.Columns[oldColumn.Name.ToString()];
   dataGridView1.Sort(newColumn,direction);
   newColumn.HeaderCell.SortGlyphDirection =
                    direction == ListSortDirection.Ascending ?
                    SortOrder.Ascending : SortOrder.Descending;

I used parts of code from : link text

kuba