Programatically sort a gridview

Hey all,

I'm trying to sort a gridview with the following code:


Protected Sub GridView1_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles GridView1.Sorting

Dim dataView1 As New Data.DataView(dt)


dataView1.Sort = e.SortExpression & " ASC"

GridView1.DataSource = dataView1
GridView1.DataBind()
End Sub


.. but it is giving me errors that it can't find the column being sorted. (IndexOutOfRangeException) I am filling datatable "dt" with an oledbdataadapter and setting it as the datasource for the gridview. This works successfully.

However after I bind the gridview, it seems like the datatable is becoming empty.. thus the error i'm getting. Please help!! It must be some tiny stupid mistake I'm making.

Thanks,
Daniel



Comments

  • Had the same problem, try this

    Session("Source") = table

    In the code where you create your source, and

    Dim dt As DataTable = CType(Session("Source"), DataTable)

    as the first line of code in your GridView1_Sorting.

    Ole

    : Hey all,
    :
    : I'm trying to sort a gridview with the following code:
    :
    :
    : Protected Sub GridView1_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles GridView1.Sorting
    :
    : Dim dataView1 As New Data.DataView(dt)
    :
    :
    : dataView1.Sort = e.SortExpression & " ASC"
    :
    : GridView1.DataSource = dataView1
    : GridView1.DataBind()
    : End Sub
    :
    :
    : .. but it is giving me errors that it can't find the column being sorted. (IndexOutOfRangeException) I am filling datatable "dt" with an oledbdataadapter and setting it as the datasource for the gridview. This works successfully.
    :
    : However after I bind the gridview, it seems like the datatable is becoming empty.. thus the error i'm getting. Please help!! It must be some tiny stupid mistake I'm making.
    :
    : Thanks,
    : Daniel
    :
    :
    :
    :

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion