ASP.net GridView Sorting Error

Murray Brown
Murray Brown used Ask the Experts™
on
In my ASP.net home page I declare the two following variables at the top of the Default.aspx.vb page
I then load data to a grid using the next piece of code. My intention is to be able to sort the data by clicking
on the column header so I included the  GridView1_Sorting procedure shown further down at the bottom.
When I click on a header I get the following error in that procedure "DataTable must be set prior to using dataview."
Public Class Default
    Private objDV As New DataView
    Private dt As New DataTable
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Open in new window


    Sub Bind_Grid_to_Dataview(ByVal oGridView As GridView, ByVal oSQL As String)

        Try

            Dim cs As String = ConfigurationManager.ConnectionStrings("SSQL").ConnectionString
            Dim objCon As New SqlConnection 'Declaring connection object
            objCon.ConnectionString = cs
            'opening connection
            If objCon.State = ConnectionState.Closed Then
                objCon.Open()
            End If
            'declaring new Data Adapter
            Dim objDA As New SqlDataAdapter
            objDA.SelectCommand = New SqlCommand(oSQL, objCon)
            Dim objDS As New DataSet
            objDA.Fill(objDS)
            Call oCheck_DataSet_For_Blank_Columns(objDS)
            'Below i am creating a default view of the Data Set
            objDV = objDS.Tables(0).DefaultView
            oGridView.DataSource = objDV
            oGridView.DataBind()

        Catch ex As Exception
            Response.Write(ex.Message & " dr2")
        End Try
    End Sub

Open in new window


   Private Sub GridView1_Sorting(sender As Object, e As GridViewSortEventArgs) Handles GridView1.Sorting

        Try
            objDV.Sort = e.SortExpression
            GridView1.DataSource = objDV
            GridView1.DataBind()
        Catch ex As Exception
            Response.Write(ex.Message & " Gridview Sorting Error 4")
        End Try

    End Sub

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Developer User Interface
Commented:
The datatable/dataview is not retained across postbacks. You will have to save the datatable in some cache like session/viewstate and then use it in Sorting event method.
Murray BrownASP.net/VSTO Developer

Author

Commented:
Hi. How would I do that?
Murray BrownASP.net/VSTO Developer

Author

Commented:
Thanks very much

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial