CRUD
OPERATION WITH GRIDVIEW IN ASP.NET C#
Step: 1 Create
database and table. Example Employee(ID, Name)
create
table Employee(
ID int
primary key,
Name
varchar(20))
Step: 2 Open
Asp.net C# Web Application
Step: 3 In Any web form of the web
application, place a GridView from Data tab in Toolbox
Step: 4 Design the Gridview, Click on Edit
Columns
Step: 5 Add
three template fields and remove the auto generate fields and add Header text
as per your table columns
Step 6: The
gridview view look like
Step 7: Click
on Edit Templates and design the columns
Step 8: There is following Column[0]-ID, Column[1]-Name and
Column[0]-Action and it has the following fields like Item Template, Alternate item
Template, EditItemTemplate, HeaderTemplate and FooterTemplate.
In ItemTemplate place a
Label to show the column value.
In EditItemTemplate add
a TextBox to edit value of the Column.
In HeaderTemplet add the
Header Text which one we want to display.
In FooterTemplate add a
TextBox to Add new records.
The following steps will
perform for all three Column[0]-ID,
Column[1]-Name and Column[0]-Action
Step: 8 In
ItemTemplate of ID field bind the value with database column
Specify the Second Column[1]-Name in Item and as one the
columns given.
In Column[2]Action add buttons to perform
operation like Edit, Delete in Item Template, Update, Cancel in Edit
ItemTemplate and Add Button in Footer Template. After that assign the
text and Command type as per button in properties window of appropriate command
buttons as show in figure after that click on End Template Editing the grid
view look like.
Select the
properties of Gridview ShowFoolter = true in properties window.
Step: 9 After performing all the design the
gridview look like
Step: 10 We have finished the design layout of
the gridview, Now we have to add some command event through gridview, so select
the gridview go to properties and select the event tab of the properties
window..
And
generate some method, on double click on Event Method show in figure…
When you
double click on the following Events the method will automatically generate on
code behind page of asp.net
Step: 11 Now the time to write the code to
connect sql server database that we have design and write code on following
generated method on code behind page in asp.net.
Add two
Namespace and write the code to connect sql server database
Namespace
using System.Data;
using
System.Data.SqlClient;
Connection Coding
SqlConnection con =
new SqlConnection("data
source=LENOVO-PC\\SQLEXPRESS;database=testdb;integrated security=true");
Code to load data into Gridview1 for this create a method and the
call the method on page load event
public void
loaddata()
{
SqlDataAdapter
adp = new SqlDataAdapter("select * from Employee", con);
DataSet
ds = new DataSet();
adp.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
Call the method on page_load event
protected void Page_Load(object
sender, EventArgs e)
{
if
(Page.IsPostBack == false)
{
loaddata();
}
}
And write the code on the following generate
methods
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs
e)
{
GridView1.EditIndex = -1;
loaddata();
}
protected void GridView1_RowCommand(object
sender, GridViewCommandEventArgs e)
{
if
(e.CommandName.Equals("Add"))
{
TextBox
TextBox1 = (TextBox)GridView1.FooterRow.FindControl("txtaddid");
TextBox
TextBox2 = (TextBox)GridView1.FooterRow.FindControl("txtaddname");
if
(TextBox1.Text == "" &&
TextBox2.Text == "")
{
Response.Write("<script>alert('Please fill all
entities')</script>");
}
else
{
con.Open();
string cmdstr = "insert
into Employee(ID,Name) values('" + TextBox1.Text + "','" + TextBox2.Text + "')";
SqlCommand
cmd = new SqlCommand(cmdstr,
con);
cmd.ExecuteNonQuery();
con.Close();
loaddata();
}
}
}
protected void GridView1_RowDeleting(object
sender, GridViewDeleteEventArgs e)
{
Label
Label1 = (Label)GridView1.Rows[e.RowIndex].FindControl("Label1");
con.Open();
string
cmdstr = "delete Employee where ID='"
+ Label1.Text + "'";
SqlCommand
cmd = new SqlCommand(cmdstr,
con);
cmd.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
loaddata();
}
protected void GridView1_RowEditing(object
sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
loaddata();
}
protected void GridView1_RowUpdating(object
sender, GridViewUpdateEventArgs e)
{
Label
Label1 = (Label)GridView1.Rows[e.RowIndex].FindControl("Label1");
TextBox
TextBox2 =(TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2");
con.Open();
string
cmdstr = "update Employee set Name='"
+ TextBox2.Text + "' where ID='" +
Label1.Text + "'";
SqlCommand
cmd = new SqlCommand(cmdstr,
con);
cmd.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
loaddata();
}
Step: 12 Now you can run the program.