Gridview row 0 selected, but data for row+1 is displayed... ??

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" 
                    <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" />


The SelectedIndexChanged event:

// Set the document number chosen:
                _DocumentNumber = gvSearchResults.SelectedDataKey


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!





1/8/2009 9:40:06 PM 72751 articles. 3 followers. Follow

4 Replies

Similar Articles

[PageSpeed] 58

Hi Tessie ,

I can not reproduce this kind of issue , but you can try another way to retrieve values ,


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.
1/12/2009 8:52:31 AM

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
1/12/2009 9:59:21 AM

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 1982034487

When 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 5

However, that didn't work either. Now I'm still getting the value of column 1, row 1 (key = 2) on this statement:

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.


1/12/2009 11:03:29 PM

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!


1/12/2009 11:26:05 PM

Similar Artilces:

new row below selected row in gridview to display child gridview
Hello,   I have a gridview that up selected..shows the nested child grivdview. The problem I currently have is that it stuffs all the data into a column whcih blows the whole tbale out of whack. What I need to do is create a new row below the sleected row to house the new child grivdview. I have seen third party controls do this such as teleriks radgrid but I wont to do this with the gridview control.  Any articles on how to do this? Does AJAX support this? Any help is greatly appreciated.    ...

Selecting a gridview row and keeping the last selected row in a gridview highlighted after mouse moves over a new row.
What is the best way to show a gridview row highlighted once selected and keep it highlighted once the mouse moves over a different row? Thanks, Steve hi,you can use <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="#FFFFFF" /> plz let me know if i misunderstood you Thanks for your help. One problem; however, is that after the row has been selected and I check the "grid.SelectedIndex" it has a value of -1 and not the index of the row that was selected on the grid. What do you recommend? Thanks, Steve hi, sorry i din...

Add new controls to a gridview row; when editing that row using the edit row feature in the gridview control.
Hi all  I have a questions that looks hard / complex to me, and i know you can help me in this. Q {  I have row in a Gridview and i have an edit option to it. I press the edit button, to edit the values in that row and update data all the way back to the database. Now when i press the edit button, i want a drop down list to be displayed that contain values for a particular field in my grid view. (for now only plain text boxes appear.) } I hope you understand my question, all i need to do is replace the text box controls with a dropdown list when i am editing a row in ...

Repeater display row w/ data when Row.count = 0 (i.e. add a row)
Hi, OK, working w/ nested repeaters - outer repeater will always have data but inner repeater may or may not have data - i.e: Record 1 (outer repeater) - Record 1a (inner repeater) - Record 1b - Record 1c Record 2 - no data Record 3 - Record 3a - Record 3b ... What I need to do is for each loop of the inner repeater where there is no data, force the inner repeater to display a single row that will be populated with previously-obtained data. This is a monthly report of active and inactive aircraft and while most will always have records in th...

Data Controls insert rows into gridview then insert rows from gridview into SQL Table
Hello there, I'm still new at performing miracles with code . Currently I am having trouble with my latest coding adventure I have a web application which records jobs and labour assigned to jobs. The issue in particular I'm having is the way in which labour is to be assigned. I want to use standard controls (Textbox, Dropdownlist) to record the values into a gridview (ie,put multiple rows into the gridview using the controls). Then, when all the inserting is complete, the user clicks a submit button to insert all the rows from the gridview into a database table. The reason for...

How to display the more than one row with items and buttons in gridview control. when i select three items at same time from categories(Fruits, Apples, Gifts). The selected items are displayed in g
Hi,when i select three items at same time from categories(Fruits, Apples, Gifts). The selected items are displayed in gridview.Here i would like to change this way "when i click more than one category at same time. it should be display selected item and button."For example: if i have to select Fruits, Apples from categories.  it should be display like 1st "row have Fruits and In 2nd row have Apples and button" in gridview.the button have some event.How can i do it? Please can anyone answer me.With kind requestThanks your query is very raw..can u details it out...Pl...

Gridview paging enabled. Has checkbox to select the row . If user select the checkbox on one page and move to 2nd page to select the rows there also to export the selected rows to excel. But doing so
HiI am using Gridview and that has paging enabled. Using checkbox to select the particular row of the gridview to export the selected rows to the excel . Everything is working fine except for one case.Let user selected the "check" box for, five rows which user would like to export to excel (two of rows on page 1 of results and 3 of them on page 2 of results), however it only exports the 3 row details from page 2, because it seems to de-select the 2 rows from page 1 when I move to page 2 and vice versa. The code i am using is as :protected void BtnExcel_Click(object sender, EventArg...

Selecting the multiple rows in GridView and inserting the selected rows to another Gridview & Performing some operations on it
 Dear All, I have become mad for solving this problem. Please Help me. I have a Gridview bound to a datasource. I inserted the CheckBox Control in Templet to select certain rows. Gridview1 contains some products id & name.I want to insert the selected rows in to second Gridview  and in the second Gridview I need to add two more colums AdvicedQty & AcceptedQty(TextBox Cotrol).The user will select few products in Gridview1 & clicks add which will be added another gridview then in second Gridview he enters the AdviceQty & AcceptedQty.After entering the data he cli...

Can you display data from a single row in multiple rows using a GridView?
I am currently displaying 3 fields in a gridview and it looks something like this:First Name Last Name Age ======== ======== === Chris Messineo 40 John Smith 35 Tim Jones 30I need to modify the gridview and display a bio, but I want it to display on 1 full line below each line, like this:First Name Last Name Age ======== ======== === Chris Messineo 40 Chris was born in a log cabin... John Smith 35 At the age of 10 John invented... Tim Jones 30 Mr. Jones was always a curio...

Problem in Using DropDownList Control to select a selected row of SqlDataSource that is under GridView Control
Hi all,In my SQL Server Management Studio Express, I have a database "ChemDatabase" that has a table "LabData"  formatted in the following way: SampleID int PRIMARY KEY, SampleName nvarchar(25), ChemicalName, nvarchar(25), Concentration decimal(6.2) with 5 rows of records in this .dbo table. In my Visual Web Developer Express, I created a website "RPD-TCE" for extracting a row of record from the LabData table by using a DropDownList, a GridView and a SqlDataSource.  I want to click DropDownList to list one number of S...

Display the footer row of the gridview control even there is no data.
Hi, I want to display the footer row of the gridview control eventhough there is no data. Is this possible. Second approach: I putted some text boxes and lables and and "Add" button in gridviews Emplytemplete mode. I want the user to enter some values in the textboxes and when he hits the "add" button it should add a row to database and rebind the gridview. How can I make it work?  Thanks in advance. SyedSyed Ali Shah______________________________________________Attention: Don't Forget to mark as "Answer" If I helped you in solving your problem. H...

Nested Gridview display below selected row of GridView?
I have a Master/Details relationship set up and it works fine with the Gridviews not nested, but I would like to be able display the details gridview below the selected row and allow the user to expand/collapse the nested gridview.  Does anybody have any source code or thoughts for something similar?  I'm really stumped.  Thanks!  Try the link below that show an example of what you want: DarmarkMark as Answer, if this reply answers your post....

How can I display the page row in a gridview
In my gridview, I have paging turned on and the pagesize set to 10. I also have a custom paging row that displays some info in the left corner and the pages in the right corner. When the gridview returns a result set with more than 10 rows everything works perfectly. However, if there are less than 10 rows the the paging row does not display (since there is only 1 page).  This means my information on the left side of the page row does not display either (not good). What can I do to force the page row to display - even if there is only 1 page of data? Thanks a lot,Chris  Can&...

Datagrid(.net 1.1) to GridView(.net 2.0) and using the RowUpdating event how to extract the values from the controls.
Ok  This is the settings: I use a Viewgrid with only itemtemplate colums for example. <asp:TemplateField HeaderText="Test"><ItemTemplate><asp:Label id="LBL_Test" text='<%# DataBinder.Eval(Container.DataItem, "Test1") %>' runat="server" meta:resourcekey="LBL_TestResource1"></asp:Label></font></ItemTemplate> <EditItemTemplate><asp:TextBox ID="HDN_Test" Text='<%# DataBinder.Eval(Container.DataItem, "Test1") %>' Runat="server" meta:resourcekey="HDN_TestResource1" /></EditItemTemplate></asp:TemplateField>  The ...

Web resources about - Gridview row 0 selected, but data for row+1 is displayed... ?? -

Portal:Law/Nominate/Selected article - Wikipedia, the free encyclopedia
Add the article in to a subpage of this portal, such as Portal:Law/Selected article/# , replacing the "#" with the number above the current highest. ...

F8: Facebook’s FbStart Initiative Will Give Selected Developers A $30K Kick-Start
... successful applications is likely a whole lot easier with $30,000 in free tools and services to start off with, and that’s exactly what selected ...

LEGO Shop - The LEGO Technic team has selected the winner... - Facebook
The LEGO Technic team has selected the winner of the Co-Creation Challenge! LEGO will be producing a limited run of 20,000 of this stunning set.... ...

baby365- Grand Doctor Ma Selected Students for iPad on the iTunes App Store
Get baby365- Grand Doctor Ma Selected Students on the App Store. See screenshots and ratings, and read customer reviews.

Selected Cheney Vice Presidential Photographs
... for photos found within these albums are Paul Morse, Karen Ballard, Kimberlee Hewitt, Shealah Craighead, and Chris Greenberg. These selected ...

[iOS Advice] How To Clarify Your Email Replies By Quoting Selected Text - YouTube
Follow me on Twitter: Like my Facebook page: RATE, COMMENT, SUBSCRIBE! :-) ...

Sait Faik Abasıyanık’s Selected Stories are tales that beguile all for their brevity
The influential Turkish writer's collection of short stories is often bizzare but never boring.

Animal of the Day: Lucius Has Been Selected as the U.K.’s ‘Happiest Cat’
He’s got one blue eye, one green eye and a heart of gold. British mobile phone company O2 recently held some “cat auditions” to find “the UK’s ...

Michael Sam becomes first openly gay player selected in an NFL Draft
Defensive end Michael Sam of the University of Missouri became the first openly gay player to be selected in a National Football League Draft ...

Refugee spinner selected for PM's XI
Fawad Ahmed has gone from asylum seeker to rubbing shoulders with Julia Gillard at The Lodge.

Resources last updated: 11/30/2015 11:56:12 PM