Link to home
Start Free TrialLog in
Avatar of Murray Brown
Murray BrownFlag for United Kingdom of Great Britain and Northern Ireland

asked on

ASP.net sort a GridView bound to a dataset

Hi

I have a GridView on my ASP.net web page that is populated with a dataset using the code below. I want to add a button that allows the user to sort by a certain column. Is the best way to sort the GridView or the dataset? What VB.net code would I use for this.



   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Page.DataBind()  
    End Sub


    Function GetData() As DataTable
        ' This method creates a DataTable with four rows.  Each row has the
        ' following schema:
        '   PictureID      int
        '   PictureURL     string
        '   Title          string
        '   DateAdded      datetime
        Dim dt As New DataTable()
        ' define the table's schema
        dt.Columns.Add(New DataColumn("PictureID", GetType(Integer)))
        dt.Columns.Add(New DataColumn("PictureURL", GetType(String)))
        dt.Columns.Add(New DataColumn("Title", GetType(String)))
        dt.Columns.Add(New DataColumn("DateAdded", GetType(DateTime)))
        ' Create the four records
        Dim dr As DataRow = dt.NewRow()
        dr("PictureID") = 1
        dr("PictureURL") = ResolveUrl("~/Resources/Craighall_Hands_On_Retreat.jpg")
        dr("Title") = "zBlue Hills"
        dr("DateAdded") = New DateTime(2005, 1, 15)
        dt.Rows.Add(dr)
        dr = dt.NewRow()
        dr("PictureID") = 2
        dr("PictureURL") = ResolveUrl("~/Resources/Craighall_Park_Abbey_Guest_House.jpg")
        dr("Title") = "Sunset"
        dr("DateAdded") = New DateTime(2005, 1, 21)
        dt.Rows.Add(dr)
        dr = dt.NewRow()
        dr("PictureID") = 3
        dr("PictureURL") = _
          ResolveUrl("~/Resources/Melrose_Melrose_Place_Guest_Lodge.jpg")
        dr("Title") = "Water Lilies"
        dr("DateAdded") = New DateTime(2005, 2, 1)
        dt.Rows.Add(dr)
        dr = dt.NewRow()
        dr("PictureID") = 4
        dr("PictureURL") = ResolveUrl("~/Resources/Melrose_The_Peech_Hotel.jpg")
        dr("Title") = "Winter"
        dr("DateAdded") = New DateTime(2005, 2, 18)
        dt.Rows.Add(dr)
        Return dt
    End Function
SOLUTION
Avatar of Obadiah Christopher
Obadiah Christopher
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Murray Brown

ASKER

Hi. Thanks, but  I updated my code as follows but it still didn't sort

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Page.DataBind()  
    End Sub

    Protected Sub btnSort_Click(sender As Object, e As EventArgs) Handles btnSort.Click
        Call SortGridView()
    End Sub

    Sub SortGridView()
        Try
            GridView1.DataSource = Nothing

            Dim dt As New DataTable()
            dt = GetData()
            Dim dv As DataView = dt.DefaultView
            dv.Sort = "Title"
            GridView1.DataSource = dv
            GridView1.DataBind()
        Catch ex As Exception

        End Try

    End Sub

    Function GetData() As DataTable
        ' This method creates a DataTable with four rows.  Each row has the
        ' following schema:
        '   PictureID      int
        '   PictureURL     string
        '   Title          string
        '   DateAdded      datetime
        Dim dt As New DataTable()
        ' define the table's schema
        dt.Columns.Add(New DataColumn("PictureID", GetType(Integer)))
        dt.Columns.Add(New DataColumn("PictureURL", GetType(String)))
        dt.Columns.Add(New DataColumn("Title", GetType(String)))
        dt.Columns.Add(New DataColumn("DateAdded", GetType(DateTime)))
        ' Create the four records
        Dim dr As DataRow = dt.NewRow()
        dr("PictureID") = 1
        dr("PictureURL") = ResolveUrl("~/Resources/Craighall_Hands_On_Retreat.jpg")
        dr("Title") = "zBlue Hills"
        dr("DateAdded") = New DateTime(2005, 1, 15)
        dt.Rows.Add(dr)
        dr = dt.NewRow()
        dr("PictureID") = 2
        dr("PictureURL") = ResolveUrl("~/Resources/Craighall_Park_Abbey_Guest_House.jpg")
        dr("Title") = "Sunset"
        dr("DateAdded") = New DateTime(2005, 1, 21)
        dt.Rows.Add(dr)
        dr = dt.NewRow()
        dr("PictureID") = 3
        dr("PictureURL") = _
          ResolveUrl("~/Resources/Melrose_Melrose_Place_Guest_Lodge.jpg")
        dr("Title") = "Water Lilies"
        dr("DateAdded") = New DateTime(2005, 2, 1)
        dt.Rows.Add(dr)
        dr = dt.NewRow()
        dr("PictureID") = 4
        dr("PictureURL") = ResolveUrl("~/Resources/Melrose_The_Peech_Hotel.jpg")
        dr("Title") = "Winter"
        dr("DateAdded") = New DateTime(2005, 2, 18)
        dt.Rows.Add(dr)
        Return dt
    End Function
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the help