Getting selectedrow in RowCommand

6.6k views Asked by At

i have a gridview as:

<asp:GridView ID="gdvOpinions" runat="server" Width="100%" CellPadding="4" 
                        ForeColor="#333333" GridLines="None" Height="100px" Visible="False" 
                        AutoGenerateColumns="False" onrowcommand="gdvOpinions_RowCommand" >
                    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                    <Columns>
                        <asp:ButtonField ButtonType="Button" CommandName="Confirm" Text="تائید" />
                        <asp:ButtonField ButtonType="Button" CommandName="Delete" Text="حذف" />
                        <asp:BoundField DataField="ID" HeaderText="ID" />
                        <asp:BoundField DataField="مقاله" HeaderText="مقاله" >
                        <ControlStyle Width="150px" />
                        </asp:BoundField>
                        <asp:BoundField DataField="نظر کاربر" HeaderText="نظر کاربر" >
                        <ControlStyle Width="250px" />
                        </asp:BoundField>
                    </Columns>
                    <EditRowStyle BackColor="#999999" />
                    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                    <SortedAscendingCellStyle BackColor="#E9E7E2" />
                    <SortedAscendingHeaderStyle BackColor="#506C8C" />
                    <SortedDescendingCellStyle BackColor="#FFFDF8" />
                    <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
                </asp:GridView>

As you see, i have two buttonfield for every row, but when i click one of these buttons and want to get their row in

gdvOpinions_RowCommand

the

gdvOpinions.SelectedRow

returns null; how should i find which row is selected?

2

There are 2 answers

3
Neeraj Singh Chouhan On BEST ANSWER
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns = "false" 
    OnRowCommand = "OnRowCommand">
 <Columns>
  <asp:ButtonField CommandName = "ButtonField"  DataTextField = "CustomerID"
        ButtonType = "Button"/>
 </Columns>
</asp:GridView>

protected void OnRowCommand(object sender, GridViewCommandEventArgs e)
{
    int index = Convert.ToInt32(e.CommandArgument);
    GridViewRow gvRow = GridView1.Rows[index]; 
}
3
Dhrumil On

From what I understand out of your question, if you are trying to get the row for which the button was clicked, then in that case this might be the right thing to do :

GridViewRow gRow = (GridViewRow)((Control)e.CommandSource).NamingContainer; 

Now here, this is for a control such as a Button or ImageButton. You might have to improvise on this for ButtonField.

You can also change the ButtonField to a simple TemplateField with a Button.

Hope this helps.