mikemardon
Programmer
Hi there
I am developing an application which has several class modules, one of which containing a sort function for a particular datagrid.
When the page is loaded, the following code happens :
___________________________________________________________
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
TheseUsers = LegUsersTable.GetAll()
DataGrid1.DataSource = TheseUsers
DataGrid1.DataKeyField = "UserID"
DataGrid1.DataBind()
End If
End Sub
___________________________________________________________
This means it is querying the sql database and returning a list of ALL users in no particular order.
When the sort button (within each column header in the template columns) is clicked, the following code happens :
___________________________________________________________
Sub DataGrid1_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles DataGrid1.SortCommand
Dim SortColumn As String
Dim SortArgument As String
If viewstate("sortdirection") Is Nothing Then
viewstate.Add("sortdirection", " ASC")
SortArgument = "ASC"
Else
If viewstate("sortdirection") = " ASC" Then
viewstate("sortdirection") = " DESC"
SortArgument = "DESC"
Else
viewstate("sortdirection") = " ASC"
SortArgument = "ASC"
End If
End If
SortColumn = e.SortExpression 'field name
SortedUsers = LegUsersTable.Sort(SortColumn, SortArgument)
TheseUsers = SortedUsers
DataGrid1.DataSource = TheseUsers
DataGrid1.DataKeyField = "UserID"
DataGrid1.DataBind()
End Sub
___________________________________________________________
This calls function 'Sort' from module 'LegUsersTable'...
___________________________________________________________
Public Function Sort(ByVal SortColumn As String, ByVal SortArgument As String)
Dim command As New SqlCommand("select * from tblUsers where Deleted = 0 order by " & SortColumn & " " & SortArgument, SQLConnection)
Dim dr As SqlDataReader = Nothing
Try
SQLConnection.Open()
dr = command.ExecuteReader(CommandBehavior.SingleResult)
Dim users As New LegUsers
While dr.Read()
users.Add(PopulateItem(dr))
End While
Return users
Finally
If Not dr Is Nothing AndAlso Not dr.IsClosed Then
dr.Close()
End If
SQLConnection.Close()
command.Dispose()
End Try
End Function
___________________________________________________________
This function queries the SQL server and orders the records by the column name of the datagrid (which equates to the fieldname in the database) and the sort argument which is either ASC or DESC.
To this point, everything is good as gold, until you click on the edit button for the sorted row in the datagrid...
It doesn't keep the correct indexing for the sorted data and consequentially, edits the row which would be there in default view.
I have read lots of messages in forums which suggest that the datagrid needs to be updated with the new indexes but have no idea where to begin!
CAN SOMEONE PLEEEEEEASSSE HELP ME OUT????
Thanks!
Mike
I am developing an application which has several class modules, one of which containing a sort function for a particular datagrid.
When the page is loaded, the following code happens :
___________________________________________________________
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
TheseUsers = LegUsersTable.GetAll()
DataGrid1.DataSource = TheseUsers
DataGrid1.DataKeyField = "UserID"
DataGrid1.DataBind()
End If
End Sub
___________________________________________________________
This means it is querying the sql database and returning a list of ALL users in no particular order.
When the sort button (within each column header in the template columns) is clicked, the following code happens :
___________________________________________________________
Sub DataGrid1_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles DataGrid1.SortCommand
Dim SortColumn As String
Dim SortArgument As String
If viewstate("sortdirection") Is Nothing Then
viewstate.Add("sortdirection", " ASC")
SortArgument = "ASC"
Else
If viewstate("sortdirection") = " ASC" Then
viewstate("sortdirection") = " DESC"
SortArgument = "DESC"
Else
viewstate("sortdirection") = " ASC"
SortArgument = "ASC"
End If
End If
SortColumn = e.SortExpression 'field name
SortedUsers = LegUsersTable.Sort(SortColumn, SortArgument)
TheseUsers = SortedUsers
DataGrid1.DataSource = TheseUsers
DataGrid1.DataKeyField = "UserID"
DataGrid1.DataBind()
End Sub
___________________________________________________________
This calls function 'Sort' from module 'LegUsersTable'...
___________________________________________________________
Public Function Sort(ByVal SortColumn As String, ByVal SortArgument As String)
Dim command As New SqlCommand("select * from tblUsers where Deleted = 0 order by " & SortColumn & " " & SortArgument, SQLConnection)
Dim dr As SqlDataReader = Nothing
Try
SQLConnection.Open()
dr = command.ExecuteReader(CommandBehavior.SingleResult)
Dim users As New LegUsers
While dr.Read()
users.Add(PopulateItem(dr))
End While
Return users
Finally
If Not dr Is Nothing AndAlso Not dr.IsClosed Then
dr.Close()
End If
SQLConnection.Close()
command.Dispose()
End Try
End Function
___________________________________________________________
This function queries the SQL server and orders the records by the column name of the datagrid (which equates to the fieldname in the database) and the sort argument which is either ASC or DESC.
To this point, everything is good as gold, until you click on the edit button for the sorted row in the datagrid...
It doesn't keep the correct indexing for the sorted data and consequentially, edits the row which would be there in default view.
I have read lots of messages in forums which suggest that the datagrid needs to be updated with the new indexes but have no idea where to begin!
CAN SOMEONE PLEEEEEEASSSE HELP ME OUT????
Thanks!
Mike