Hello. Here's a rather interesting dilemma--hopefully easy to solve? Though I'm stumped for the moment, so any help will be greatly appreciated.
I have a gridview with master records listed. The records are acquired using L2S and a sproc that applies Row_Number() to the results for custom paging. The resultset looks fine. It binds to the gridview as expected (displaying data for rows 1 through 5). Each master record has a "select" column to allow the user to select which master record to display in the detail record view. When select is clicked, the child records are retrieved from the database. So far, so good...
However, when I click on the select hyperlink for gridview row 0, I get the data for gridview row 1. I've checked the selectedIndex in the debugger, and it says 0. When I parse the gridview row[0] in the Immediate Window, the data for row 1 is displayed--even though the index is 0, and I can see the data for row index 0 in the gridview on the web page. The SelectedIndexChanged event uses the SelectedDataKey[key] to set the parameter values for the child record look up, and it always shows the data for gridview row index+1 selected. I looked at the HTML generated for the gridview table, and it looks fine. When hovering over each select hyperlink, it shows the correct row index number for postback (from 0 to 4 for the first five records). I've posted some code below....
The gridview (the RowBound event just does some ProperCase string formatting):
<asp:GridView ID="gvSearchResults" runat="server" AutoGenerateColumns="False" DataKeyNames="VOCERT, VOODCD, STDSLN, GGNAME, RowNumber" RowStyle-HorizontalAlign="Center" onrowdatabound="SearchResults_RowDataBound" onselectedindexchanged="SearchResults_SelectedIndexChanged"> <Columns> <asp:BoundField DataField="RowNumber" HeaderText="" /> <asp:BoundField DataField="VOCERT" HeaderText="Document Number" /> <asp:BoundField DataField="VOODCD" HeaderText="Document Date" /> <asp:BoundField DataField="STDSLN" HeaderText="Document Type" /> (etc.)The SelectedIndexChanged event:
// Set the document number chosen:
_DocumentNumber = gvSearchResults.SelectedDataKey
[HelperMethods.DocumentDataColumn.DocumentNumber].ToString();
(etc.)In the index changed event above, it shows the correct index value but with index+1 data.
Any insight or suggestions are welcome. Many thanks!
Tess
Tess
![]() |
0 |
![]() |
Hi Tessie ,
I can not reproduce this kind of issue , but you can try another way to retrieve values ,
this
.GridView1.SelectedRow.Cells[i].Text;
Samu Zhang
Microsoft Online Community Support
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
![]() |
0 |
![]() |
Sounds like a potential problem with your data not the gridview. Maybe something is wrong with the query? Can you provide a screen shot of the results?
David Fowler
SDE, ASP.NET Team, Microsoft
![]() |
0 |
![]() |
Hello, I tried the SelectedRow.Cells[1].Text suggestion, but that came up with the same results. The selectedIndex is 0, but the data is for row index 1.
I can't post any screen shots, unfortunately (no server out in the DMZ).I can show some debug.print information, however. I added loops through the datakey collection after binding and after selecting.
When the gridview is first loaded, the data keys are (correctly):
key is 1979014367
key is 1979014368
key is 1981050395
key is 1981050395
key is 1982034487When the first row is selected (the one with 1979014367 as the key), the second row (with 1979014368) is displayed and the datakey set shifts forward one record (showing the sixth record and dropping off the first record).
As you can see, the datakeys are not unique. I'm working with a legacy data model which does not incorporate unique identifiers :-(.
Could that be the problem? To try a different approach, I eliminated the duplicated VOCERT column from the datakeys collection and just used the value of Row_Number() (from the query). This way, each row in the grid has a unique key.key is 1
key is 2
key is 3
key is 4
key is 5However, that didn't work either. Now I'm still getting the value of column 1, row 1 (key = 2) on this statement:
gvSearchResults.SelectedDataKey.Values["RowNumber"].ToString()I had a look at gvSearchResults.DataKeys[0][0] after the Select event, and it shows key = 2 (the second record). Before the select is fired, this value is 1 (as it should be).
I've set a breakpoint at the SelectedIndexChanged event to see what's happening. The SelectedIndex property is 0 when I click on the first row in the grid, but when I use that index value in, for example, string docNumber = gvSearchResults.Rows[gvSearchResults.SelectedIndex].Cells[1].Text, it also shows the data for the second row.
Any suggestions are most welcome! Thanks very much.
ps: I'll debug the queries and make sure they're functioning correctly.
Tess
![]() |
0 |
![]() |
Yes! The query was the problem--a parameter was off. Wow... when something is off by only one, it can sure make my head hurt.
Thanks very much!cheers,
Tess
Tess
![]() |
0 |
![]() |