How can I sort programatically DataGrid ADO.NET

  • 12 years ago

    I am writing a program in using I have such a problem:
    how can I sort DataGrid programatically???

    The problem is that I merge few datasets and get one so I can't sort it using 'order by' in sql.

    Please help.

    Thanks in advance for your help.
  • 12 years ago

    The DefaultView property of a DataTable is a DataView.  When you bind a control to a DataTable you are actually binding it to this DataView.  You can set the RowFilter, RowStateFilter and, most interestingly for you, Sort properties of the DataView to affect the data displayed in the control.  The Sort property of the DataView class takes a valid SQL ORDER BY clause without the words "ORDER BY" at the start.  Here is an example of sorting a bound DataGrid by the "Name" column:

    myDataGrid.DataSource = myDataTable
    myDataTable.DefaultView.Sort = "Name ASC"
    The DataViewManager class is to the DataSet as the DataView class is to the DataTable.  A DataSet has a DefaultViewManager property that is a DataViewManager object.

Post a reply

Enter your message below

Sign in or Join us (it's free).


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“The generation of random numbers is too important to be left to chance.” - Robert R. Coveyou