Click to See Complete Forum and Search --> : [RESOLVED] Datagridview sorting


soandos
September 2nd, 2009, 08:22 PM
How can one have the data in the Datagridview sorted based on a column without the user clicking on anything?

thank you

using .net 3.5

rliq
September 2nd, 2009, 11:50 PM
Check out the DataTable.Select(String filterExpression, String sort) member. Set the filterExpression = String.Empty and specify the field that you want to sort by in the sort parameter. Eg:

myDataTable.Select(String.Empty, "LastName");

Then bind myDataTable to your DataGridView.

If you are getting the data from a database via SELECT or calling a STORED PROCEDURE, then consider using an ORDER BY clause to sort your data earlier in the process.