Winforms- click on Datagrid view column Header should sort data (both ways)

Posted by Madhavi under C# on 3/2/2010 | Views : 23505 | Status : [Member] | Replies : 3
Hi

My datagrid views is having 5 columns. When i click on specific "column Header" it should sort all the data in ascending and if I click the same column should sort in descending order. Click on other columns header should not allow sorting. How to accomplish this. Can any one help on this.

Also how to capture the selected column header which is selected for sort.



Thanks in advance

Madhavi




Responses

Posted by: Sagarp on: 3/2/2010 [Member] Bronze

Up
0
Down
Check following code
private void dgOrders_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)

{

// Check which column is selected, otherwise set NewColumn to null.
DataGridViewColumn newColumn =
dgOrders.SelectedColumns.Count > 0 ?
dgOrders.SelectedColumns[0] : null;

DataGridViewColumn oldColumn = dgOrders.SortedColumn;
ListSortDirection direction;

// If oldColumn is null, then the DataGridView is not currently sorted.
if (oldColumn != null)
{
// Sort the same column again, reversing the SortOrder.
if (oldColumn == newColumn &&
dgOrders.SortOrder == SortOrder.Ascending)
{
direction = ListSortDirection.Descending;
}
else
{
// Sort a new column and remove the old SortGlyph.
direction = ListSortDirection.Ascending;
oldColumn.HeaderCell.SortGlyphDirection = SortOrder.None;
}
}
else
{
direction = ListSortDirection.Ascending;
}

dgOrders.Sort(newColumn, direction);

newColumn.HeaderCell.SortGlyphDirection =
direction == ListSortDirection.Ascending ?
SortOrder.Ascending : SortOrder.Descending;
}


Ref this links

http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_23424796.html

Thanks
SagarP
http://www.emanonsolutions.net
http://emanonsolutions.blogspot.com/

Madhavi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Madhavi on: 3/2/2010 [Member] Starter

Up
0
Down
Hi ,

setting the datagridcolumns sortMode to "Automatic" is working fine.In both directions.

But problem is it is sorting only the displayed rows ( 5 rows in grid are allowed to view) not all the rows. Ex: Out of 10 rows it will sort only first 5 rows


But how can i achieve all the rows to be sorted in one click

Madhavi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Madhavi on: 3/4/2010 [Member] Starter

Up
0
Down
Hi


Problem:

In datagrid view , Click on header column sorting only current pages rows.

But , my requirement is to sort all the data , not the current page data. Page


Can any one help ?


Thanks
Madhavi

Madhavi, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response