C# Getting cell value on button click for each row on gridview

17,007

Add CommandArgument='<%# Container.DataItemIndex %>' to your button and in your code behind you can get the row that raised the event.

 <asp:Button ID="btnSendInvite" runat="server" Text="Send Invite" CommandName="sendInvite"  CommandArgument='<%# Container.DataItemIndex %>'/> 

Get the row in your code behind

protected void grdCreateGroup_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "sendInvite")
            {
                int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow row = grdCreateGroup.Rows[index];

                  Button b = (Button)row.FindControl("btnSendInvite");
                   b.Text = row.Cells[0].Text;

            }
}
Share:
17,007
xenazfire
Author by

xenazfire

Updated on June 05, 2022

Comments

  • xenazfire
    xenazfire almost 2 years

    I have gridview with buttons on it. I want the buttons on each row to get the value of the first cell in the gridview for each row. So for example the table below:

    | Header 1 | Header 2 | Header 3 |
    | Happy | Sad | Button 1 |
    | Worry | Angry | Button 2 |
    | Excited | Frown | Button 3 |

    When I press button 1 it will show Happy. If I press button 2 it will show Worry. So on and so forth. Need your help on how do I achieve this.

    .HTML

    <asp:gridview id="grdCreateGroup" runat="server" DataKeyNames="id" AutoGenerateColumns="false" OnRowCommand="grdCreateGroup_RowCommand" OnRowDataBound="grdCreateGroup_RowDataBound">
                <Columns>
                    <asp:BoundField DataField="admissionNo" HeaderText="Admission No." />
                    <asp:BoundField DataField="fullName" HeaderText="Full Name" />
                    <asp:templatefield headertext="">
                        <itemtemplate>
                            <asp:Button ID="btnSendInvite" runat="server" Text="Send Invite" CommandName="sendInvite" /> 
                        </itemtemplate>
                    </asp:templatefield>
                </Columns>
            </asp:gridview>
    

    .CS

    protected void grdCreateGroup_RowCommand(object sender, GridViewCommandEventArgs e)
            {
                if (e.CommandName == "sendInvite")
                {
                    account accounta = new account();
                    account accountb = new account();
                    accountManager accountManager = new accountManager();
                    invite invite = new invite();
                    inviteManager inviteManager = new inviteManager();
                    string emailAddress, admissionNo;
                    bool status = true;
    
                    emailAddress = HttpContext.Current.User.Identity.Name;
                    accounta = accountManager.getAccInfoByEmailAddress(emailAddress);
    
                    invite.leaderName = accounta.fullName;
                    invite.groupNo = accounta.groupNo;
                    //invite.recipientEmailAddress = accountb.recipientEmailAddress;
    
                    //status = inviteManager.sendInvite(invite);
    
                    GridViewRow Row = grdCreateGroup.Rows[0];
                    Button btnSendInvite = (Button)Row.FindControl("btnSendInvite");
    
                    if (status == true)
                    {
                        divMessage.InnerHtml = invite.groupNo.ToString();
    
                        btnSendInvite.Text = "Invite Sent";
                        btnSendInvite.Enabled = false;
                    }
                    else
                    {
                        divMessage.InnerHtml = "Record not added in database";
                    }
                }
            }