>For sorting on a datagrid - do you HAVE to use a dataview?
Yes.
>I used the msdn example and now get his error:
>
>DataTable must be set prior to using DataView
>
>So I have a dataset created from the input of the user and an SQL
>statement. Then I use a dataview - well because I have to. The
>dataview was made at design time along with the textbox and label.
>
>Here is the code to generate and display my data retrieved:
>
>Private Sub btnQuery_Click(ByVal sender As System.Object, ByVal e As
>System.EventArgs) Handles btn.Click
>
>
> Dim Count As Integer
> Dim ds As New DataSet
> Dim SQL As String
> SQL = "select [First],[Last],[Title],[Authors],[Address] from
>DB1 WHERE [Title] LIKE '%" & txt.Text & "%'" & "OR [Address] LIKE '%" &
>txt.Text & "%'" & " ORDER BY [Authors]"
> Dim da As New SqlDataAdapter(SQL, SqlConnection2)
>
> Try
> SqlConnection2.Open()
>
> da.Fill(ds)
> Session("ds") = ds
>
> Dim dv As DataView = ds.Tables(0).DefaultView
>
> Count = dv.Count
>
> dg.DataSource = dv
> dg.DataBind()
> lblCount.Text = Count
>
> Catch ex As Exception
> txtQuery.Text = ex.Message.ToString
> Finally
> SqlConnection2.Close()
> End Try
> End Sub
>
>Private Sub dg_SortCommand(ByVal source As Object, ByVal e As
>System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles
>dg.SortCommand
> dv.Sort = e.SortExpression
> dg.DataBind()
> End Sub
Try changing to this:
Private Sub dg_SortCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles
dg.SortCommand
Dim dv As DataView = ds.Tables(0).DefaultView
dv.Sort = e.SortExpression
dg.DataSource = dv
dg.DataBind()
End Sub
Or better yet, put your databinding logic into a separate routine, all
call that from your Button_Click, your sort event, and your paging
events.
Marcie
|