gridview row command

G

Guest

How do I find out the row index selected in the gridveiw row command method?

I have an imageButton in a gridview cell and its corresponding code in the
row command method needs to know the row index in question in order to set
the background of the cell to a different color. But the selectedIndex value
doesnt return a value as expected.

Is there something I am missing?
 
P

Pipo

You can use the SelectedDataKey property to retrieve the DataKey object for
the currently selected row. You can also use the SelectedValue property to
retrieve the data key value for the currently selected row directly.
 
P

Pipo

not?
Search for the row with the same id and you'll know the row, right?
(row.FindControl("imagebutton") for the imagebutton or search for the cell
where the imagebutton is in.
 
G

Guest

The SelectedDataKey only holds the datakey of the datasource, this is not the
same as the row index, the datakey could be a customerID which is not going
to be the same as the row index. Or am I mistaken?
 
P

Pipo

No you are right, sorry
What I always do is that I have a column (visible = false) with the unique
record ID.
With that I can find the row which was selected.
I was in the wrong assumpsion you did that also.
 
Joined
Apr 17, 2007
Messages
1
Reaction score
0
You can access the row index from within the RowCommand by using the e.CommandArgument property. "e" is the passed GridViewCommandEventArgs parameter to the RowCommand method. GridView will automatically set the CommandArgument inside this parameter to the index of the row where the command occurred (which may NOT neccessarily be the "selected" row!).

int index = Convert.ToInt32(e.CommandArgument);

Hope this helps.
 
Joined
Dec 8, 2009
Messages
1
Reaction score
0
Reply is here blogs. ipona.com /davids/archive/2007/11/22/8447.aspx:

<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="IncreasePriceButton" runat="server"
Text="+10%"
CommandName="IncreasePrice"
CommandArgument='<%# Container.DataItemIndex %>' />
<asp:Button ID="DecreasePriceButton" runat="server"
Text="-10%"
CommandName="DecreasePrice"
CommandArgument='<%# Container.DataItemIndex %>' />
</ItemTemplate>
</asp:TemplateField>
 
Joined
Feb 4, 2008
Messages
2
Reaction score
0
nikitagon said:
Reply is here blogs. ipona.com /davids/archive/2007/11/22/8447.aspx:

<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="IncreasePriceButton" runat="server"
Text="+10%"
CommandName="IncreasePrice"
CommandArgument='<%# Container.DataItemIndex %>' />
<asp:Button ID="DecreasePriceButton" runat="server"
Text="-10%"
CommandName="DecreasePrice"
CommandArgument='<%# Container.DataItemIndex %>' />
</ItemTemplate>
</asp:TemplateField>


Asp:buttonfield does the exact same thing with less markup
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top