Click here to Skip to main content
15,888,285 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
VB
Protected Sub SortRecords(sender As Object, e As GridViewSortEventArgs)
        Dim dv As New DataView(GridView1.DataSource)
       
        Dim SortDir As String = String.Empty
        Dim sortExpression As String = e.SortExpression

        If dv IsNot Nothing Then
            If direction = SortDirection.Ascending Then
                direction = SortDirection.Descending

                SortDir = "Desc"
                ViewState("SortExpression") = Convert.ToString(e.SortExpression & " " & SortDir)
            Else
                direction = SortDirection.Ascending

                SortDir = "Asc"
                ViewState("SortExpression") = Convert.ToString(e.SortExpression & " " & SortDir)

            End If

        End If
        'BindGridView()
        dv.Sort = ViewState("SortExpression").ToString 'error happen here 
'DataTable must be set prior to using DataView
        GridView1.DataSource = dv
        GridView1.AllowSorting = True
        GridView1.DataBind()

    End Sub


What I have tried:

I've tried bound the data to datatable instead of the dataview at first but same error still happen.
Posted
v3

1 solution

I think you are doing it wrong. Refer - GridView.Sorting Event (System.Web.UI.WebControls)[^].
 
Share this answer
 
Comments
Member 12474994 28-Apr-16 3:09am    
I've already check it out but the way they bind the data is different compared to mine. I bind each column with different sql command under different sub.
for example:
Private Sub BindBpn()

Using con As New SqlConnection(ConfigurationManager.ConnectionStrings("SQLServer2005DBConnectionString").ToString())
Using cmd As New SqlCommand("SELECT * FROM LOT WHERE BASEPRODUCTNAME='" & txtbpn.Text & "'order by checkin asc")
Using sda As New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Dim dt As New DataTable()
sda.Fill(dt)
Dim dv As DataView = dt.DefaultView
GridView1.DataSource = dv
GridView1.DataBind()
End Using
End Using
End Using
If GridView1.Visible = False Then
GridView1.Visible = True
End If
If Button1.Visible = False Then
Button1.Visible = True
End If
End Sub
and
Private Sub Bindbt()

Using con As New SqlConnection(ConfigurationManager.ConnectionStrings("SQLServer2005DBConnectionString").ToString())
Using cmd As New SqlCommand("SELECT * FROM LOT WHERE MBTBoardNo='" & TextBox2.Text & "'order by checkin asc")
Using sda As New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Dim dt As New DataTable()
sda.Fill(dt)
Dim dv As DataView = dt.DefaultView
GridView1.DataSource = dv
GridView1.DataBind()


End Using
End Using
End Using
If GridView1.Visible = False Then
GridView1.Visible = True
End If
If Button1.Visible = False Then
Button1.Visible = True
End If
End Sub
Have you debugged and checked if the DataSource is null or having value in the below line?

Dim dv As New DataView(GridView1.DataSource)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900