How can I sort programatically DataGrid ADO.NET

  • 15 years ago
    Hello,

    I am writing a program in vb.net using ado.net. 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.
  • 15 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:

    Code:
    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).

Contribute

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.

“UNIX is basically a simple operating system, but you have to be a genius to understand the simplicity.” - Dennis Ritchie