|
-
Jun 11th, 2003, 02:23 PM
#1
Thread Starter
PowerPoster
DataGrid and Sorting
Here is my scenario:
I want to use the in memory Data ONLY for the sorting...NO requerying of the database.
On Page load event I use a DataSet to DataBind to the DataGrid.
Then when I sort I use a DataView to sort then try to bind to the DataGrid using the DataView but the Page load event is firing again. So, how do I do a simple sort?
Code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
SqlDataAdapter1.SelectCommand.Parameters("@strDepartment").Value = "web"
SqlDataAdapter1.Fill(Ds1)
DataGrid1.DataBind()
End If
End Sub
Private Sub DataGrid1_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles DataGrid1.SortCommand
Dim SortExpression As String = e.SortExpression.ToString()
dv.Sort = SortExpression
DataGrid1.DataSource = dv
DataGrid1.DataBind()
End Sub
End Class
-
Jun 11th, 2003, 04:09 PM
#2
Frenzied Member
In the Sort procedure you have to fill the DataSet again, or you can save the DataSet in a Session variable in the Page Load procedure, if you dont want to hit the DB again.
-
Jun 12th, 2003, 08:12 AM
#3
Thread Starter
PowerPoster
I tried this but still the datagrid comes up empty after I call the DataBind in the Sort Event.
Code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
SqlDataAdapter1.SelectCommand.Parameters("@strDepartment").Value = "web"
SqlDataAdapter1.Fill(Ds1)
DataGrid1.DataBind()
Cache.Insert("ds", Ds1, Nothing, DateTime.Now.AddMinutes(5), Caching.Cache.NoSlidingExpiration)
End If
End Sub
Private Sub DataGrid1_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles DataGrid1.SortCommand
Dim newdv As DataView = New DataView(Cache("ds"))
Dim SortExpression As String = e.SortExpression.ToString()
newdv.Sort = SortExpression & " asc"
DataGrid1.DataSource = newdv
DataGrid1.DataBind()
End Sub
End Class
-
Jun 12th, 2003, 09:45 AM
#4
Thread Starter
PowerPoster
I got it to work!
Code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Grab cache and place into DataView
dv = Cache("ds")
'If cache is EMPTY
If dv Is Nothing Then
'Fill it up
SqlDataAdapter1.SelectCommand.Parameters("@strDepartment").Value = "web"
SqlDataAdapter1.Fill(Ds1)
'Set DataView from the DataSet
dv = New DataView(Ds1.Tables("GetAllContentByDept"))
'Put DataView into cache
Cache("ds") = dv
End If
DataGrid1.DataSource = dv
DataGrid1.DataBind()
End Sub
Private Sub DataGrid1_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles DataGrid1.SortCommand
'Grab cache and place into DataView
dv = Cache("ds")
'Column Sorted
Dim SortExpression As String = e.SortExpression.ToString()
'Column and Direction ex. 'Column1 DESC'
Dim strDGSortOrder As String = dv.Sort.ToString
Dim strSortX As String = "ASC"
'If ASC is found in the string
If strDGSortOrder.IndexOf("ASC") > 0 Then
strSortX = "DESC"
End If
'Build sorting string
dv.Sort = SortExpression & " " & strSortX
'Set DataSource
DataGrid1.DataSource = dv
'Re-bind
DataGrid1.DataBind()
End Sub
End Class
-
Jun 12th, 2003, 10:12 AM
#5
Frenzied Member
The SortExpression returns a string, so you dont have to call the ToString() method
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|