Gridview row selection after sorting

sscguy

New member
Joined
Jan 11, 2007
Messages
2
Programming Experience
5-10
I’m having a problem with the Gridview control. I populate the gridview. I’ve set up code so it will sort when I click on the column headings; alternating between ascending or descending. It appears correctly in the gridview after sorting. When I select a row in the gridview I can programmatically get the selected index number. But when I pull the values for the selected index, the values are not for the row I selected. They are for the row that was originally in that position before sorting.
It seems like the SelectedIndex property is actually getting sorted with the row, so when I select the first row, I get selected index of 0, but the values in the row are for the row that was in that 0 position before sorting. Can anyone tell me what I’m doing wrong? I have some code examples and screen shots in a Word document but it’s about 7 pages long so I didn’t want to post it here unless someone needed to see it.
 
Moved thread to more relevant Web Grids section. Your post doesn't specify win grid or web grid and I am assuming web grid since you stated gridview and not datagridview. It's always a good idea to be as specific as possible, that way you'll receive better help.

If you'll post the relevant code (showing how you "programmatically get the selected index number" and "pull the values for the selected index") someone may be able to find the problem. The SelectedIndex does not generally get sorted with the row.
 
Code for my original question

Here is the code that goes with my question. I start out by populating a dataset object with one table, and then binding the dataset to the gridview by setting the DataSource property to mRegRuns (my dataset), and then using the DataBind() method. Then in my latest attempt to get this to work, I store the dataset in the ViewState, and use that to sort, and bind the sorted dataset to the gridview control. Here is my sorting code:

VB.NET:
[SIZE=2][FONT=Courier New][COLOR=blue]Protected[/COLOR] [COLOR=blue]Sub[/COLOR] grdRunData_Sorting([COLOR=blue]ByVal[/COLOR] sender [COLOR=blue]As[/COLOR] [COLOR=blue]Object[/COLOR], [COLOR=blue]ByVal[/COLOR] e [COLOR=blue]As[/COLOR] System.Web.UI.WebControls.GridViewSortEventArgs) [COLOR=blue]Handles[/COLOR] grdRunData.Sorting[/FONT][/SIZE]
[SIZE=2][FONT=Courier New][COLOR=green]' Retrieve the data source from session state.[/COLOR][/FONT][/SIZE]
[SIZE=2][FONT=Courier New][COLOR=blue]Dim[/COLOR] ds [COLOR=blue]As[/COLOR] DataSet = ViewState([COLOR=maroon]"mRegRuns"[/COLOR])[/FONT][/SIZE]
[SIZE=2][FONT=Courier New][COLOR=blue]Dim[/COLOR] dt [COLOR=blue]As[/COLOR] DataTable = ds.Tables(0)[/FONT][/SIZE]
[SIZE=2][FONT=Courier New][COLOR=blue]Dim[/COLOR] dv [COLOR=blue]As[/COLOR] DataView = [COLOR=blue]New[/COLOR] DataView(dt)[/FONT][/SIZE]
[SIZE=2][FONT=Courier New][COLOR=blue]If[/COLOR] ViewState([COLOR=maroon]"sortdirection"[/COLOR]) = e.SortExpression & [COLOR=maroon]","[/COLOR] & [COLOR=maroon]"ASC"[/COLOR] [COLOR=blue]Then[/COLOR][/FONT][/SIZE]
[SIZE=2][FONT=Courier New]dv.Sort = e.SortExpression & [COLOR=maroon]" DESC"[/COLOR][/FONT][/SIZE]
[SIZE=2][FONT=Courier New]ViewState([COLOR=maroon]"sortdirection"[/COLOR]) = e.SortExpression & [COLOR=maroon]","[/COLOR] & [COLOR=maroon]"DESC"[/COLOR][/FONT][/SIZE]
[SIZE=2][FONT=Courier New][COLOR=blue]Else[/COLOR][/FONT][/SIZE]
[SIZE=2][FONT=Courier New]dv.Sort = e.SortExpression & [COLOR=maroon]" ASC"[/COLOR][/FONT][/SIZE]
[SIZE=2][FONT=Courier New]ViewState([COLOR=maroon]"sortdirection"[/COLOR]) = e.SortExpression & [COLOR=maroon]","[/COLOR] & [COLOR=maroon]"ASC"[/COLOR][/FONT][/SIZE]
[SIZE=2][FONT=Courier New][COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR][/FONT][/SIZE]
 
[SIZE=2][FONT=Courier New]grdRunData.DataSource = dv[/FONT][/SIZE]
[SIZE=2][FONT=Courier New]grdRunData.DataBind()[/FONT][/SIZE]
[SIZE=2][FONT=Courier New][COLOR=blue]End[/COLOR] [COLOR=blue]Sub[/COLOR][/FONT][/SIZE]
 
[SIZE=2]
[/SIZE]

After I sort, the data in the gridview appears sorted, but if I select a row and get the SelectedIndex property, and pull data from the "selected" row, the data is actually from the row that used to be in that position in the gridview before I sorted.

VB.NET:
[FONT=Courier New][COLOR=blue]Protected[/COLOR] [COLOR=blue]Sub[/COLOR] grdRunData_SelectedIndexChanged([COLOR=blue]ByVal[/COLOR] sender [COLOR=blue]As[/COLOR] [COLOR=blue]Object[/COLOR], [COLOR=blue]ByVal[/COLOR] e [COLOR=blue]As[/COLOR] System.EventArgs) [COLOR=blue]Handles[/COLOR] grdRunData.SelectedIndexChanged[/FONT]
[FONT=Courier New][COLOR=blue]Dim[/COLOR] strSelRunNum [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR][/FONT]
[FONT=Courier New][COLOR=blue]Dim[/COLOR] currentIndex [COLOR=blue]As[/COLOR] [COLOR=blue]Integer[/COLOR][/FONT]
[FONT=Courier New]currentIndex = grdRunData.SelectedIndex[/FONT]
[FONT=Courier New]strSelRunNum = [COLOR=blue]CStr[/COLOR](grdRunData.DataKeys(currentIndex).Value)[/FONT]
 
[FONT=Courier New][COLOR=green][FONT=Courier New]'Also tried the following with the same results.[/FONT][/COLOR]
[COLOR=green][FONT=Courier New]'strSelRunNum[/FONT][/COLOR][COLOR=green][FONT=Courier New] = grdRunData.SelectedRow.Cells.Item(1).Text.ToString[/FONT][/COLOR]
[/FONT]
[FONT=Courier New][FONT=Courier New]Session([COLOR=maroon]"RunNumber"[/COLOR]) = strSelRunNum[/FONT]
 
 
[/FONT][FONT=Courier New][COLOR=green]' redirect the user to the EditList page[/COLOR][/FONT]
 
 
[FONT=Courier New]Response.Redirect([COLOR=maroon]"Edits.aspx"[/COLOR])[/FONT]
[FONT=Courier New][COLOR=blue]End[/COLOR] [COLOR=blue]Sub[/COLOR][/FONT]

I'm new to the gridview control, and it's been a while since I've coded in VB so if it looks like I'm doing something strange, that's why. But we want to rewrite a lot of old programs and the gridview is going to be very useful if I can figure out how to get it to do what I want. Thanks in advance.


 
Back
Top