Search code examples
c#asp.netwebforms

How do I get the row I clicked with a button


I have a grid view and when I click on the button I wanted to do a SQL command to see which Order number was pressed. How do I get the row I have clicked on?

asp

<asp:GridView ID="GridView1" runat="server" 
          DataKeyNames="No_" AutoGenerateColumns="false"  style="color:Black;border-collapse:collapse;margin-right: auto;display: table;text-align: center;" OnPageIndexChanging="MyGrid_PageIndexChanging" OnRowDataBound="MyGrid_RowDataBound" AllowPaging="True"  PageSize="20" AllowCustomPaging="False" >

        <Columns>
           <asp:BoundField DataField="No_" HeaderText="No_Encomenda"  />
           <asp:BoundField DataField="[Item No_]" HeaderText="Item number"  />
           <asp:BoundField DataField="Quantity" HeaderText="Quantity"  />

                        <asp:TemplateField HeaderText="Select">
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBox1" runat="server" OnCheckedChanged="CheckBox1_CheckedChanged" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
             <PagerSettings Mode="NumericFirstLast" PageButtonCount="4" FirstPageText="First" LastPageText="Last"/>  
             <PagerStyle CssClass="gridview"  HorizontalAlign="Center" VerticalAlign="Middle"/>
      </asp:GridView>

cs

protected void ButtonDetails_Click(object sender, EventArgs e)
        {
            MyGrid.Visible = false;
            GridView1.Visible = true;
        }

Gridview image: enter image description here


Solution

  • First of all, in your markup, I don't see a button in your markup. So the picture you have, and the markup don't agree with each other.

    There are about 3 great ways to get the row click with a button.

    Most common:

    Drop in a the button. Say like this:

      <asp:TemplateField HeaderText="View">
         <ItemTemplate>
            <asp:Button ID="cmdView" runat="server" Text="Details" CommandName="Select" />
         </ItemTemplate>
      </asp:TemplateField>
    

    Note how we added the CommandName = "select". This is a special option that will trigger the Gridivew RowIndexed changed. It ALSO triggers the row command. But, in row command the "selected row event" has not yet triggered. So, I just ignore the row command event, and put your code in the selected index changed event.

      protected void MyGrid_SelectedIndexChanged(object sender, EventArgs e)
        {
    
                // Get selected row
                GridViewRow dG = MyGrid.SelectedRow;
                Response.Write("Row index was " + dG.RowIndex.ToString());
    
                // get non templated columns - they appear in cells
                Response.Write("2nd colum value = " + dG.Cells[1].Text);
    
                // get templated fields - say a combo box for city
                DropDownList CityComboBox = (DropDownList)dG.FindControl("DropDownList1");
                Response.Write("City selected from combo box = " + CityComboBox.SelectedValue);
    
    
    
            }
    

    So by JUST adding the CommandName = "Select", then this will trigger the SelectedIndexChanged event.

    Another way is to use the row command (but you have to pass the row index to that event).

    A really slick way is to 100% ignore the grid events, and JUST use your button.

    You can do it this way:

    Drop in the button. But now you can't double click on the button to wire up the click event, but you CAN STILL set the event for the click.

    While in the markup you can thus type OnClick=, NOTE VERY carefully the IntelliSense that pops up - it looks like this:

    enter image description here

    So in the above choices - choose the create new event - it SEEMS like nothing occurred, but when you flip back to code-behind, you have a nice simple button click event.

    Droop a button - create click event. You can now just 100% ignore the complex GridView events (and that select command etc.).

    You do this way now:

      protected void Button3_Click(object sender, EventArgs e)
        {
    
            Button MyButton = (Button)sender;
            GridViewRow dG = (GridViewRow)MyButton.Parent.Parent;
    
    
            Response.Write("Row index was " + dG.RowIndex.ToString());
    
            // get non templated columns - they appear in cells
            Response.Write("2nd colum value = " + dG.Cells[1].Text);
    
            // get templated fields - say a combo box for city
            DropDownList CityComboBox = (DropDownList)dG.FindControl("DropDownList1");
            Response.Write("City selected from combo box = " + CityComboBox.SelectedValue);
        }
    

    I find the above is less hassle, but also LESS learning curve to use. We drop in a button - click event. You just pick up the "sender" into a button, and then get parent.parent which turns out to be the grid row we want.

    The first parent is I think some cell divider. In fact I use this .parent trick all the time. Thus buttons are a simple button dropped into the grid markup, and we use the standard button click event and code approach.

    But, hey, we really don't care about the GridView row command, and we really don't care about the Selected index changed.

    We have a button click. So now you can use this slick trick to save using complex GridView events, and just code up a simple button like any other button.

    And we get FULL USE of the grid view row with this trick.