3

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";
                }
            }
        }

2 Answers 2

2

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;

            }
}
Sign up to request clarification or add additional context in comments.

Comments

0
protected void btnSendInvite_OnClick(object sender,EventArgs e)
{
  Button btn=(Button)sender;
  GridViewRow row=(GridViewRow)btn.NamingContainer;
  string name=row.Cells[1].Text; // here you will get Name 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.