• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 728
  • Last Modified:

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
0
Murray Brown
Asked:
Murray Brown
3 Solutions
 
informaniacCommented:
Dim dt As New DataTable()

dt = GetData()

Dim dv As DataView = dt.DefaultView

dv.Sort = "SortColumn"

gv.DataSource = dv;

gv.DataBind();
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
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
0
 
CodeCruiserCommented:
See if changing

dv.Sort = "Title"

to

dv.Sort = "Title ASC"

makes any difference.
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thanks for the help
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now