Sort datagridview associated with a List

  • Thread starter Thread starter Paul
  • Start date Start date
P

Paul

Hi,

If I associate a DataGridView to a LIST <>, the Grid not sorted by
clicking on the header.
Any idea to do this?

Thanks
 
Paul said:
Hi,

If I associate a DataGridView to a LIST <>, the Grid not sorted by
clicking on the header.
Any idea to do this?

Thanks

Not sure about winforms.
With web datagridview you only automatically get column sorting when binding
to a dataset.
Bit of a pain really because you'd be nuts to bind to a dataset for a web
app.

Anyhow, you need to write your own IComparer.
http://paigecsharp.blogspot.com/2007/07/generic-list-sorting.html

Then intercept the sorting event on your gridview, sort using your
genericcomparer and rebind.

Then your next question will be how you get a column to reverse it's
direction.
You have to know what the direction was previously.

----
public SortDirection GridViewSortDirection

{

get

{

// Checks for the first time when the ViewState sort direction is null

if (ViewState["sortDirection"] == null)

ViewState["sortDirection"] = SortDirection.Ascending;

// Changes the sort direction

else

{

if (((SortDirection)ViewState["sortDirection"]) == SortDirection.Ascending)

{

ViewState["sortDirection"] = SortDirection.Descending;

}

else

{

ViewState["sortDirection"] = SortDirection.Ascending;

}

}

return (SortDirection)ViewState["sortDirection"];

}

set

{

ViewState["sortDirection"] = value;

}

}
 
Back
Top