Sorting a DataTable.Select into a Grid with or without using a DataView

5. May 2009

Read this article in your language IT | EN | DE | ES

I was debating with a friend on having to use a DataView to sort data from a DataTable. I had mentioned you could use the DataTable.Select function to sort your data without a DataView.  However, binding this data back to the GridView requires you to put the filtered rows back into a DataTable. So we went on to discuss which is better.

Normally, I would use the code below to loop through my filtered rows to manipulate them in some way, however this post is just to sort them without a DataView.

I’m using AdventureWorks database and pulling from Production.Product table.

So which is better? Without a DataView? DataTable to DataRow() to NewDataTable To GridView

OR

With a DataView? DataTable to DataView to GridView

Private _ConnectionString As String = String.Empty
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        PopulateGridViewWithoutDataView()
        PopulateGridViewWithDataView()
    End Sub
 
    Private Sub PopulateGridViewWithoutDataView()
        Try
            Dim dtOriginalProductList As DataTable = GetListOfProducts.Tables(0) 'Get the datatable of products
            Dim drProducts() As DataRow = dtOriginalProductList.Select("1=1", "ListPrice DESC") 'select 1=1 brings back all rows, and then just sort
            Dim dtNewProductList As DataTable = dtOriginalProductList.Clone 'create datatable to hold new list (to bind to gridview)
            For Each drProduct As DataRow In drProducts 'Put new list of data into the new datatable 
                dtNewProductList.ImportRow(drProduct)
            Next
            gvProductsWithoutDataView.DataSource = dtNewProductList 'Set
            gvProductsWithoutDataView.DataBind() 'Bind
        Catch ex As Exception
            'Error handling code here
        End Try
    End Sub
 
    Private Sub PopulateGridViewWithDataView()
        Dim dtOriginalProductList As DataTable = GetListOfProducts.Tables(0) 'Get the datatable of products
        Dim dvProductList As New DataView 'Create DataView
        dvProductList.Table = dtOriginalProductList 'Load DataTable
        dvProductList.Sort = "ListPrice DESC" 'Sort Property
        gvProductsWithDataView.DataSource = dvProductList 'Set
        gvProductsWithDataView.DataBind() 'Bind
    End Sub
 
    Public Function GetListOfProducts() As DataSet
        Dim oDS As New DataSet
        Try
            _ConnectionString = ConfigurationManager.ConnectionStrings("DefaultConnectionString").ConnectionString
            oDS = DataAccess.SqlHelper.ExecuteDataset(_ConnectionString, CommandType.Text, "SELECT ProductID, Name, ProductNumber, StandardCost, ListPrice FROM Production.Product")
            Return oDS
        Catch ex As Exception
            Throw ex
        Finally
        End Try
    End Function

PopulateGridViewWithoutDataView – This method binds products to the gridview that have been sorted by using DataTable.Select  Benefits I see is that even though you have to import the new rows into a another datatable before binding, you can then manipulate this new datatable anyway you see fit. Ex. Pulling a certain row back after it’s been sorted.

PopulateGridViewWithDataView – This method binds products to the gridview that are sorted via a DataView and then bound to the GridView. This method is great as well, except if you want to manipulate the data or pull rows from the datatable by index based on the sorted view. This will reference the real row in the original datatable and not from the dataview itself. So if you need to manipulate the data, then go without using a dataview. However, if you don’t have to touch the data or pull certain rows, then use the DataView method.

Code Snippets, VB.NET , , , ,

Add comment


(Will show your Gravatar icon)

  Country flag

Click to change captcha
biuquote
  • Comment
  • Preview
Loading