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;

}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top