Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Datagrid - using sorting and editing

Status
Not open for further replies.

mikemardon

Programmer
May 23, 2005
71
GB
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
 
What happens if you trace the data key in the itemdatabound event?

tigerjade

"Always code as if the person who ends up maintaining your code will be a violent psychopath who knows where you live." -- Martin Golding


 
Thanks for the response TigerJade.

I am new to vs and have not used tracing before, are there any steps I need to take ie. configuration wise before I can use this?

Thanks again

Mike
 
A couple of little ones. Put Trace="True" in your declaration statement at the very top. Then, just throw some Trace.Write statements in your ItemDataBound code where the appropriate values are created; they'll appear on the page beneath the 'real' code.

tigerjade

"Always code as if the person who ends up maintaining your code will be a violent psychopath who knows where you live." -- Martin Golding


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top