locked
Boundfield value in Gridview RowCommand RRS feed

  • Question

  • User-1499457942 posted

    Hi

      How to get Boundfield value in Gridview RowCommand event with Boundfield name.

    Thanks

    Monday, September 24, 2018 5:17 PM

All replies

  • User839733648 posted

    Hi  JagjitSingh ,

    I suggest you could use Datakeys to find the value.

    You should first set the  DataKeyNames in the GridView.

    Then you may get the value you want in the GridView1_RowCommand event using the following statement.

    int rowIndex = Convert.ToInt32(e.CommandArgument);
    string value = GridView1.DataKeys[rowIndex].Values["BoundfieldName"].ToString();
    

    For more details, you could refer to the code below.

    .aspx

                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand" 
                    DataKeyNames="Eid,Ename">
                    <Columns>
                        <asp:BoundField DataField="Eid" HeaderText="Eid" SortExpression="Eid" />
                        <asp:BoundField DataField="Ename" HeaderText="Ename" SortExpression="Ename" />
                        <asp:BoundField DataField="age" HeaderText="age" SortExpression="age" />
                        <asp:BoundField DataField="sex" HeaderText="sex" SortExpression="sex" />
                        <asp:CommandField HeaderText="Operation" ShowDeleteButton="True" ShowEditButton="True"/>
                    </Columns>
                </asp:GridView>
    

    code behind.

            protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
            {
                int rowIndex = Convert.ToInt32(e.CommandArgument);
                string EidTxt = GridView1.DataKeys[rowIndex].Values["Eid"].ToString();
                string EnameTxt = GridView1.DataKeys[rowIndex].Values["Ename"].ToString();
            }
    

    result(when clicking the row where Eid=7 ):

    Best regards,

    Jenifer

    Tuesday, September 25, 2018 6:37 AM
  • Monday, October 8, 2018 11:50 AM