Click here to Skip to main content
15,899,026 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have a gridview in my web application. The gridview is filled with records from my Db. I have a button field on each row containing records. i want to update records in my db whenever a button in the griview is clicked. for example, If i have something like this
SN NAME ADDRESS PHONE SignOut Action
1 Mike abc 0984 Logout(this is a button field). When the "Log out" button is clicked, i want to update that same row in my database. i need to be able to get the value (1) so that i can update the signOut field in my db

What I have tried:

Here is my gridview

ASP.NET
<pre lang="ASP.NET"><asp:GridView ID="GridView1" runat="server" AllowPaging="True" Height="326px" OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="5" style="text-align: left; margin-left: 169px" Width="1069px" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnRowDataBound="GridView1_RowDataBound" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand" OnRowEditing="GridView1_RowEditing">

                     <Columns>
                         <asp:BoundField HeaderText="S/N" DataField="SN" />
                         <asp:BoundField HeaderText="First Name" DataField="FirstName" />
                         <asp:BoundField HeaderText="Address" DataField="Address" />
                         <asp:BoundField HeaderText="Phone Number" DataField="PhoneNumber" />
                         <asp:BoundField HeaderText="Sex" DataField="Sex" />
                         <asp:BoundField HeaderText="Reason" DataField="Reason" />
                         <asp:BoundField HeaderText="SignIn" DataField="SignIn_Time" />
                         <asp:BoundField HeaderText="SignOut" DataField="Signout_Time" />

                         <asp:TemplateField HeaderText="Action" Visible="True">
                             <ItemTemplate>
                                 <asp:Button ID="out" runat="server" Text="Sign out" CommandName="SignOut"/>
                             </ItemTemplate>
                         </asp:TemplateField>
                     </Columns>

             <PagerSettings FirstPageText="First" LastPageText="Last" Mode="NumericFirstLast" PageButtonCount="5" />


         </asp:GridView>

my code behind
C#
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
       {
          if (e.CommandName == "SignOut")
           {
              int index = Convert.ToInt32(e.CommandArgument);
              GridViewRow Grid = GridView1.Rows[index];


}
Posted
Updated 10-Oct-16 6:28am
v2

Don't use commandName(Delete it) if you want defined your own, example set "DataKeyNames" for fields "SN" and "Sex" (depend what field value you want to take),
ASP.NET
<asp:GridView ID="GridView1" runat="server" DataKeyNames="SN,Sex" AllowPaging="True" Height="326px" OnPageIndexChanging="GridView1_PageIndexChanging" >
 <asp:TemplateField>
      <ItemTemplate>
      <asp:Button ID="TestClick" Text="Test" runat="server" OnClick="Unnamed_Click"  />
     </ItemTemplate>
</asp:TemplateField>
</asp:GridView>


Code Behind:
protected void Unnamed_Click(object sender, EventArgs e)
{
try
{
Button TestClick = sender as Button;
GridViewRow rowlist = TestClick.NamingContainer as GridViewRow;
var rowindex = rowlist.RowIndex;
var SNField = this.GridView1.DataKeys[rowindex]["SN"].ToString();
var SexField = this.GridView1.DataKeys[rowindex]
["Sex"].ToString();
}
catch (Exception ex)
{

}
}
 
Share this answer
 
v4
While you could do it in RowCommand, you could also do it at the Click event handler of the Button like this:

C#
protected void out_Click(object sender, EventArgs e)
{
        Button b = (Button)sender;
        GridViewRow row = (GridViewRow)b.NamingContainer;
        if (row != null)
        {
            int index = row.RowIndex; //gets the row index selected
            int ID = Convert.ToInt32(row.Cells[0].Text); 

            //your update code here
        }

       //Make sure to rebind your grid here with the data from the datasource to reflect the changes
} 


Then make sure you wired-up the event handler for your Button to trigger the event:

ASP.NET
<asp:button id="out" runat="server" text="Sign out" onclick="out_Click">
</asp:button>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900