Method for Grid Update in ASP.Net with C#
ASPX page
<asp:GridView ID="grd" runat="server" CellPadding="4" AutoGenerateColumns="False" OnRowCancelingEdit="grd_RowCancelingEdit" OnRowEditing="grd_RowEditing" OnRowUpdating="grd_RowUpdating" AllowPaging="true" PageSize="15" OnPageIndexChanging="grd_PageIndexChanging" CssClass="ui-data-grid" OnSelectedIndexChanged="grd_SelectedIndexChanged" Width="707px">
<AlternatingRowStyle CssClass="gridAltrows" />
<SelectedRowStyle CssClass="gridSelected" />
<Columns>
<asp:TemplateField HeaderText="S.No." HeaderStyle-CssClass="col-center" ItemStyle-CssClass="col-center">
<ItemTemplate>
<asp:Label ID="lblrowno" runat="server" Text='<%# Container.DataItemIndex + 1 %>'></asp:Label>
<asp:HiddenField ID="hdnkey_ID" runat="server" Value='<%#Eval("key_Id") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Role Name" HeaderStyle-CssClass="col-center" ItemStyle-CssClass="col-center">
<ItemTemplate>
<asp:Label ID="lblKeyword_name" runat="server" Text='<%#Eval("Keyword_name") %>' ItemStyle-Width="40%" HeaderStyle-Font-Bold="true" ></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtKeyword_name" runat="server" Text='<%#Eval("Keyword_name") %>' MaxLength="50"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-CssClass="col-center" HeaderText="Action">
<ItemTemplate>
<asp:Button ID="btnEdit" runat="server" Text="Edit" CssClass="button-style" Width="100px" CommandName="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="btnUpdate" runat="server" Text="Update" CssClass="button-style" CommandName="Update" />
<asp:Button ID="btnCancel" runat="server" Text="Cancel" CssClass="button-style" CommandName="Cancel" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
.CS Page
public void BindGridView()
{
try
{
MySqlConnection con = new MySqlConnection(conStr);
{
con.Open();
string str = "SELECT Sr_No,key_Id,Keyword_name,added_on FROM keyword_mst";
MySqlCommand cmd = new MySqlCommand(str, con);
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
grd.DataSource = ds;
grd.DataBind();
if (ds.Tables[0].Rows.Count > 0)
{
lblRecords.Text = "Total Record/s :" + " " + ds.Tables[0].Rows.Count.ToString();
lblRecords.Visible = true;
}
grd.DataSource = ds;
grd.DataBind();
con.Close();
}
}
catch (Exception)
{
throw;
}
}
protected void grd_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
grd.EditIndex = -1;
BindGridView();
}
protected void grd_RowEditing(object sender, GridViewEditEventArgs e)
{
grd.EditIndex = e.NewEditIndex;
BindGridView();
}
protected void grd_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
HiddenField IdNo = grd.Rows[e.RowIndex].FindControl("hdnkey_ID") as HiddenField;
TextBox KeywordName = grd.Rows[e.RowIndex].FindControl("txtKeyword_name") as TextBox;
string Query = "Update keyword_mst set Keyword_name='" + KeywordName.Text + "' where key_Id='" + IdNo.Value+"'";
MySqlConnection con = new MySqlConnection(conStr);
con.Open();
//updating the record
MySqlCommand cmd = new MySqlCommand(Query, con);
cmd.ExecuteNonQuery();
con.Close();
//Setting the EditIndex property to -1 to cancel the Edit mode in Gridview
grd.EditIndex = -1;
//Call ShowData method for displaying updated data
BindGridView();
}
protected void grd_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grd.PageIndex = e.NewPageIndex;
BindGridView();
}