How to Pass Data key from grid view to server side RRS feed

  • Question

  • User-683124725 posted

    Hello :)

    I have a grid view, which contain a button in each row

    I want to do something with the data key (the primary key ) of the grid when I press a button in some row

    can I retrieve the data key for used row to use it in my C# code ?

    thank you all :)

    Tuesday, April 24, 2012 6:08 PM


  • User-1528094326 posted

    Are you using built in buttons like Select, Edit, Delete? Or are you using a custom button? If you are using your own button then I would just suggest using the Command options.

    <asp:GridView ID="MyGridView" runat="server" onrowcommand="MyGridView_RowCommand">
                    <asp:Button ID="MyCoolButton" runat="server" Text="Click Me To Get ID" CommandName="MyCommand" CommandArgument='<%# Eval("ID") %>' />
    // C# Code Behind
    protected void MyGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        if (e.CommandName == "MyCommand")
    int primaryKey = Convert.ToInt32(e.CommandArgument); // do stuff with your primary key value here. }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 24, 2012 6:22 PM