Mojtaba Khooryani
Mojtaba Khooryani

Reputation: 1875

get CommandArgument from GridViewRow asp webform

is it possible to get CommandArgument from GridViewRow?

in the for-each loop in button click event I need CommandArgument

example code:

foreach (GridViewRow row in MyGridView.Rows)
   {
       ...
       string commandArgument = ?;
       ...
   }

Upvotes: 0

Views: 74

Answers (1)

Diwas Poudel
Diwas Poudel

Reputation: 847

According to your comments I understood your mission . You can solve this by creating Hidden Field for getting Id of table and you can access this id when the checkbox is checked. Then do update on the basis of id. Here I am updating name column of grid view.

Sample: WebForm1.aspx

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand" OnRowEditing="GridView1_RowEditing">
            <Columns>
                <asp:TemplateField HeaderText="name">
                    <ItemTemplate>
                        <asp:Label ID="lblUpdate" runat="server" Text= '<%#Eval("name") %>'></asp:Label> 
                        <asp:CheckBox ID="chkbox" runat="server" />
                         <asp:HiddenField ID="HiddenField1" runat="server" Value='<%#Eval("ID") %>' />
                    </ItemTemplate>
                </asp:TemplateField>

WebForm1.aspx.cs

protected void btnSave_Click(object sender, EventArgs e)        {

        foreach (GridViewRow row in GridView1.Rows)
        {

            if (row.RowType==DataControlRowType.DataRow)
            {
                CheckBox chkbox = (CheckBox)row.Cells[0].FindControl("chkbox");
                HiddenField hdID = (HiddenField)row.Cells[0].FindControl("id");
                Label lbl = (Label)row.Cells[0].FindControl("lblUpdate");
                if (chkbox!=null)
                {
                    if(chkbox.Checked)
                    {
                        string Id = hdID.Value;
                        //now update name...  here by the help of this RowId===> Id
                    }
                }
            }

        }

Note here Cells[0] means first TemplateField data. And I have used this because I have placed all name field and checkbox field in first templatefield.

I think this will help you. :)

Upvotes: 1

Related Questions