In this article i will explain how to insert edit update delete and canceling operations in grid view
You can achieve by using the
1.onrowediting-Is used to Edit the particular row
2.onrowupdating-Is used to update the particular row
3.onrowdeleting-Is used to delete the particular row
4.onrowcancelingedit-Is used to cancel the particular row
code in .aspx page:
You can achieve by using the
1.onrowediting-Is used to Edit the particular row
2.onrowupdating-Is used to update the particular row
3.onrowdeleting-Is used to delete the particular row
4.onrowcancelingedit-Is used to cancel the particular row
code in .aspx page:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
AutoGenerateEditButton="True" onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating" AutoGenerateDeleteButton="True"
onrowdeleting="GridView1_RowDeleting" DataKeyNames="Id"
onrowcancelingedit="GridView1_RowCancelingEdit">
<Columns><asp:TemplateField HeaderText="Id"><ItemTemplate><asp:Label ID="lbl_id" runat="server" Text='<%#Eval("Id")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate><asp:TextBox ID="txt_id" runat="server" Text='<%#Eval("Id")%>'></asp:TextBox>
</EditItemTemplate>
</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="Salary"><ItemTemplate><asp:Label ID="lbl_sal" runat="server" Text='<%#Eval("salary")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate><asp:TextBox ID="txt_salary" runat="server" Text='<%#Eval("salary")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Department"><ItemTemplate><asp:Label ID="lbl_dept" runat="server" Text='<%#Eval("Department")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate><asp:TextBox ID="txt_dept" runat="server" Text='<%#Eval("Department")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<table><tr><td>Id:</td><td>
<asp:TextBox ID="txt_id" runat="server"></asp:TextBox></td></tr>
<tr><td>Name:</td><td>
<asp:TextBox ID="txt_name" runat="server"></asp:TextBox></td></tr>
<tr><td>Salary:</td><td>
<asp:TextBox ID="txt_salary" runat="server"></asp:TextBox></td></tr>
<tr><td>Department:</td><td>
<asp:TextBox ID="txt_dept" runat="server"></asp:TextBox></td></tr>
<tr><td colspan="2" align="center">
<asp:Button ID="btn_submit" runat="server" Text="Submit"
onclick="btn_submit_Click" /></td></tr>
</table>
</div>
</form>
create table in sql server
column name datatype
Id Int(set identity property =true)
Name Nvarchar(100)
Salary Nvarchar(50)
Deprtment Nvarchar(50)
code in .aspx.cs page:
Add namespaces
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
string strcon = ConfigurationManager.ConnectionStrings["sqlconn"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
getdata();
}
}
//To bind the data from database to grid view
public void getdata()
{
SqlConnection con = new SqlConnection(strcon);
SqlCommand cmd = new SqlCommand("select * from Emp", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource=ds;
GridView1.DataBind();
}
//To edit the the row event
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
getdata();
}
//To update the row event
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = GridView1.Rows[e.RowIndex] as GridViewRow;
int Id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values["Id"].ToString());
TextBox tname = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txt_name");
TextBox tsalary = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txt_salary");
TextBox tdept = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txt_dept");
SqlConnection con = new SqlConnection(strcon);
SqlCommand cmd = new SqlCommand("update Emp set Name=@Name,salary=@salary,Department=@Department"+" where Id=@Id", con);
cmd.Parameters.AddWithValue("@Name", tname.Text.Trim());
cmd.Parameters.AddWithValue("@salary",tsalary.Text.Trim());
cmd.Parameters.AddWithValue("@Department", tdept.Text.Trim());
cmd.Parameters.AddWithValue("@Id", Id);
con.Open();
cmd.ExecuteNonQuery();
GridView1.EditIndex = -1;
getdata();
con.Close();
}
//To delete the row event
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int Id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values["Id"].ToString());
SqlConnection con = new SqlConnection(strcon);
SqlCommand cmd = new SqlCommand("delete from Emp where Id=@Id", con);
con.Open();
cmd.Parameters.AddWithValue("@Id", Id);
cmd.ExecuteNonQuery();
GridView1.DataBind();
con.Close();
getdata();
}
//To canceling the row
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
getdata();
}
protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
}
//To insert the data and bind into gridview as well as data base
protected void btn_submit_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strcon);
con.Open();
SqlCommand cmd = new SqlCommand("emp_insert_sp", con);
cmd.CommandType = CommandType.StoredProcedure;
//cmd.Parameters.AddWithValue("@Id", txt_id.Text);
cmd.Parameters.AddWithValue("@Name", txt_name.Text);
cmd.Parameters.AddWithValue("@salary", txt_salary.Text);
cmd.Parameters.AddWithValue("@Department", txt_dept.Text);
cmd.ExecuteNonQuery();
con.Close();
Name Nvarchar(100)
Salary Nvarchar(50)
Deprtment Nvarchar(50)
code in .aspx.cs page:
Add namespaces
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
string strcon = ConfigurationManager.ConnectionStrings["sqlconn"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
getdata();
}
}
//To bind the data from database to grid view
public void getdata()
{
SqlConnection con = new SqlConnection(strcon);
SqlCommand cmd = new SqlCommand("select * from Emp", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource=ds;
GridView1.DataBind();
}
//To edit the the row event
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
getdata();
}
//To update the row event
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = GridView1.Rows[e.RowIndex] as GridViewRow;
int Id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values["Id"].ToString());
TextBox tname = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txt_name");
TextBox tsalary = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txt_salary");
TextBox tdept = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txt_dept");
SqlConnection con = new SqlConnection(strcon);
SqlCommand cmd = new SqlCommand("update Emp set Name=@Name,salary=@salary,Department=@Department"+" where Id=@Id", con);
cmd.Parameters.AddWithValue("@Name", tname.Text.Trim());
cmd.Parameters.AddWithValue("@salary",tsalary.Text.Trim());
cmd.Parameters.AddWithValue("@Department", tdept.Text.Trim());
cmd.Parameters.AddWithValue("@Id", Id);
con.Open();
cmd.ExecuteNonQuery();
GridView1.EditIndex = -1;
getdata();
con.Close();
}
//To delete the row event
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int Id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values["Id"].ToString());
SqlConnection con = new SqlConnection(strcon);
SqlCommand cmd = new SqlCommand("delete from Emp where Id=@Id", con);
con.Open();
cmd.Parameters.AddWithValue("@Id", Id);
cmd.ExecuteNonQuery();
GridView1.DataBind();
con.Close();
getdata();
}
//To canceling the row
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
getdata();
}
protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
}
//To insert the data and bind into gridview as well as data base
protected void btn_submit_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strcon);
con.Open();
SqlCommand cmd = new SqlCommand("emp_insert_sp", con);
cmd.CommandType = CommandType.StoredProcedure;
//cmd.Parameters.AddWithValue("@Id", txt_id.Text);
cmd.Parameters.AddWithValue("@Name", txt_name.Text);
cmd.Parameters.AddWithValue("@salary", txt_salary.Text);
cmd.Parameters.AddWithValue("@Department", txt_dept.Text);
cmd.ExecuteNonQuery();
con.Close();
I hope you understand how to insert ,edit ,update ,delete and canceling in gridview using asp.net
No comments:
Post a Comment