This Article shows how to Edit and Update record in a Gridview in ASP.NET. This example is helpful in situations where an administrator have to edit the several records from the database. For demonstration I have created a database (named Database.mdf) in which we have a table named tbl_Employee.
Table Schema used in this example:
Let's Begin:
1) Drop a GridView Control from the toolbox and set AutoGenerateColumns to false.
2) Add the Columns Collection (tag) that manages the collection of column fields.
3) Add TemplateField inside the Columns Collection that is used to display custom content in a data-bound control.
4) Add an ItemTemplate in the TemplateField that specifies the content to display for the items in a TemplateField.
5) Add an EditItemTemplate in the TemplateField that specifies a custom user interface (UI) for the item in edit mode.
6) Set Command name property to Edit in Edit buttton, Update in Update button and Cancel in Cancel Button according to their respective Events.
7) Add OnRowEditing, OnRowUpdating, OnRowCancelingEdit Event in Gridview.
Default.aspx Code:
Default.aspx.cs Code:
Final Preview:
Hope you like it. Thanks.
Table Schema used in this example:
CREATE TABLE [dbo].[tbl_Employee] (
[ID] INT NOT NULL,
[Name] VARCHAR (50) NULL,
[City] VARCHAR (50) NULL,
PRIMARY KEY CLUSTERED ([ID] ASC)
);
|
1) Drop a GridView Control from the toolbox and set AutoGenerateColumns to false.
2) Add the Columns Collection (tag) that manages the collection of column fields.
3) Add TemplateField inside the Columns Collection that is used to display custom content in a data-bound control.
4) Add an ItemTemplate in the TemplateField that specifies the content to display for the items in a TemplateField.
5) Add an EditItemTemplate in the TemplateField that specifies a custom user interface (UI) for the item in edit mode.
6) Set Command name property to Edit in Edit buttton, Update in Update button and Cancel in Cancel Button according to their respective Events.
7) Add OnRowEditing, OnRowUpdating, OnRowCancelingEdit Event in Gridview.
Default.aspx Code:
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1"
runat="server" AutoGenerateColumns="False" CellPadding="6" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btn_Edit" runat="server" Text="Edit" CommandName="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="btn_Update"
runat="server" Text="Update" CommandName="Update"/>
<asp:Button ID="btn_Cancel"
runat="server" Text="Cancel" CommandName="Cancel"/>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lbl_ID" runat="server" Text='<%#Eval("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lbl_Name" runat="server" Text='<%#Eval("Name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_Name" runat="server" Text='<%#Eval("Name") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City">
<ItemTemplate>
<asp:Label ID="lbl_City" runat="server" Text='<%#Eval("City") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_City" runat="server" Text='<%#Eval("City") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#663300" ForeColor="#ffffff"/>
<RowStyle BackColor="#e7ceb6"/>
</asp:GridView>
</div>
</form>
|
using System;
using System.Data;
using
System.Data.SqlClient;
using
System.Configuration;
using
System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
//Connection String from web.config File
string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
SqlConnection con;
SqlDataAdapter
adapt;
DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
ShowData();
}
}
//ShowData method for Displaying Data in Gridview
protected void ShowData()
{
dt = new DataTable();
con = new SqlConnection(cs);
con.Open();
adapt = new SqlDataAdapter("Select ID,Name,City from tbl_Employee",con);
adapt.Fill(dt);
if(dt.Rows.Count>0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
con.Close();
}
protected void
GridView1_RowEditing(object sender, System.Web.UI.WebControls.GridViewEditEventArgs e)
{
//NewEditIndex property used to determine the index of
the row being edited.
GridView1.EditIndex = e.NewEditIndex;
ShowData();
}
protected void
GridView1_RowUpdating(object sender, System.Web.UI.WebControls.GridViewUpdateEventArgs
e)
{
//Finding the controls from Gridview for the row which
is going to update
Label
id=GridView1.Rows[e.RowIndex].FindControl("lbl_ID") as Label;
TextBox name =
GridView1.Rows[e.RowIndex].FindControl("txt_Name") as TextBox;
TextBox city =
GridView1.Rows[e.RowIndex].FindControl("txt_City") as TextBox;
con = new SqlConnection(cs);
con.Open();
//updating the record
SqlCommand cmd =
new SqlCommand("Update tbl_Employee set Name='"+name.Text+"',City='"+city.Text+"'
where ID="+Convert.ToInt32(id.Text),con);
cmd.ExecuteNonQuery();
con.Close();
//Setting the EditIndex property to -1 to cancel the
Edit mode in Gridview
GridView1.EditIndex = -1;
//Call ShowData method for displaying updated data
ShowData();
}
protected void GridView1_RowCancelingEdit(object sender,
System.Web.UI.WebControls.GridViewCancelEditEventArgs e)
{
//Setting the EditIndex property to -1 to cancel the
Edit mode in Gridview
GridView1.EditIndex = -1;
ShowData();
}
}
|
Hope you like it. Thanks.
[Download Source Code via Google Drive]
Watch Video:
IT DOESN'T RUN THIS CODE
ReplyDeletethanks, really appreciate this write up, but in a case of updating a row in datagridview that is connected to a database and the ID of each row is auto-generated by mssql, how will i reference my updating row in the code. thanks
ReplyDelete