2

I have Gridview with some records (1 record=1 row).

On every row I added a button to delete this record from my mysql db.

Every row has the same button.

The problem is I need to know in which row the button was clicked? I need this to get a row index to get id of my record which is in that row.

How can I do this in easiest way?

Gridview:

        <asp:GridView ID="GridView1" runat="server" 
        CellPadding="6" EnableModelValidation="True" ForeColor="#333333" 
        GridLines="None" Caption="TWOJE WIZYTY" Font-Bold="True" 
        onrowcreated="GridView1_RowCreated" style="text-align: left">
        <AlternatingRowStyle BackColor="#AEAEAE" />
        <EditRowStyle BackColor="Blue" />
        <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#868686" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="Blue" ForeColor="#333333" HorizontalAlign="Center" />
        <RowStyle BackColor="#C7C7C7" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
    </asp:GridView>

The button is being added like that:

        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        Button b1 = new Button();
        b1.Text = "usuń";
        b1.OnClientClick = "return potwierdzenie()";
        b1.Click+=new EventHandler(b1_Click);
        TableCell cel = new TableCell();
        cel.Width = Unit.Pixel(180);
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[0].Visible = false;
            e.Row.Cells[1].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[2].Text = "";
            e.Row.Cells.Add(cel);
        }
        else
        {
            //HERE IS MY BUTTON ADDED! *********************
            cel.Controls.Add(b1);
            cel.HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[0].Visible = false;
            e.Row.Cells[1].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Left;
            e.Row.Cells.Add(cel);
        }
    }
Paweł Adamczyk
  • 223
  • 3
  • 6
  • 14
  • Can you update the question with the markup that you are using for the GridView? Depending on how you declared the button the solution may be different. – Icarus Jan 11 '13 at 23:18
  • Where is the id of the record? Are you using C# or VB.NET? – Tim Schmelter Jan 11 '13 at 23:18
  • Have a look at the first answer of [this question](http://stackoverflow.com/questions/10795325/datagridview-selection-changed-event-and-deleting-selected-rows-from-database) to get an idea - hope that helps. – Chait Jan 11 '13 at 23:19

1 Answers1

8

You can use the NamingContainer property of your button to get the GridViewRow. Then you have all you need to find your other controls (f.e. the control with ID if you use TemplateFields).

protected void button_Delete_click(Object sender, EventArgs e)
{
    Button btn = (Button) sender;
    GridViewRow row = (GridViewRow) btn.NamingContainer;
    // assuming you store the ID in a Hiddenield:
    Hiddenield hiddenID = (HiddenField) row.FindControl("HiddenID");
    int ID = int.Parse(hiddenID.Value);
    // delete the record
}

You can also get the row-index via row.RowIndex.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • @PawełAdamczyk: Since you have edited your question now and showed that you don't use template-fields, you cannot use `FindControl` then. But you can use `row.RowIndex` to get the index if that helps.(edited my answer acc.) – Tim Schmelter Jan 11 '13 at 23:29