Pages

Wednesday, November 9, 2011

Sort DataSet records using a DataView object (VB .NET)

In .NET, you can easily sort DataSet records using a DataView. First, create a new DataSet and load it. Then, create a new DataView object using a DataTable in the DataSet. Finally, identify the column or columns you want to sort.

The following code snippet illustrates the process:

Dim objDv as New DataView(objDataSet.Tables(0))
objDv.Sort = "Column1"

This code creates a DataView object using the first table in the DataSet and sorts the view on the column named Column1. To sort on multiple columns, use a comma-delimited list, like so:

objDv.Sort = "Column1, Column3"

If the unsorted DataSet is bound to a control, such as a DataGrid, you'll need to bind the DataView to that object in order to see the newly sorted data. You can use code similar to the following:

DataGrid1.DataSource = objDv

See more information about .NET courses and certifications.


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.