Grid view save edit and update
This is a simple code for inserting,updating,and deleting the data in Gridview .
this is a simple code for editing,updating and deleting in gridviewEstablish the Connection
This is the procedure for establishing the sql connection
string connection = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlDataAdapter DA;
DataSet ds = new DataSet();ToBind Data
this is the format of binding the data to the grid
Here a Connection 'con' is instantiated and the connecction 'con' is opened
and closed inbetween the open and close lies the code for binding grid.
public void Bindgrid()
{
SqlConnection con = new SqlConnection(connection);
con.Open();
DA = new SqlDataAdapter("Select * from Category", con);
DA.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}
in your gridView property set show footer to 'TRUE"To Save
to Save data the code is as follows
protected void BtnSave(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(connection);
con.Open();
SqlCommand cmd = new SqlCommand("insert into Category values('" + TextBox1.Text + "','" + TextBox2.Text + "')", con);
//Bindgrid();
cmd.ExecuteNonQuery();
con.Close();
Bindgrid();
}TO Update
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
TextBox textcategory = (TextBox)row.Cells[2].FindControl("TextBox4");
TextBox textDescription = (TextBox)row.Cells[3].FindControl("TextBox6");
SqlConnection con = new SqlConnection(connection);
con.Open();
SqlCommand cmd = new SqlCommand("update Category set Description='" + textDescription.Text + "' where categoryName='" + textcategory.Text + "'", con);
cmd.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
Bindgrid();
}Canceling Edit
this is nothing but to cancel the edit
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
Bindgrid();
}
the procedure for editing is as follows
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
Bindgrid();
}TO Delete
here is the snippet for deleting the data in the gridview
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
//TextBox textcategory = (TextBox)row.FindControl("TextBox4");
Label lbldeleteID = (Label)row.FindControl("Label1");
SqlConnection con = new SqlConnection(connection);
con.Open();
SqlCommand cmd = new SqlCommand("delete from Category where categoryName='" + lbldeleteID.Text + "'", con);
cmd.ExecuteNonQuery();
Bindgrid();
con.Close();
}To insert Element using RowCommand
this is an alternate method to insert a data using through the gridview row
and it is as follows
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
TextBox txtcategoryName = (TextBox)GridView1.FooterRow.FindControl("TextBox8");
TextBox txtDescription = (TextBox)GridView1.FooterRow.FindControl("TextBox7");
//Label lblDescription = (Label)GridView1.FooterRow.FindControl("Label2");
SqlConnection con = new SqlConnection(connection);
con.Open();
SqlCommand cmd = new SqlCommand("insert into Category values ('"+txtcategoryName.Text+"','"+txtDescription.Text+"')",con );
cmd.ExecuteNonQuery();
con.Close();
Bindgrid();
}
The sample Code is as follows
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
public partial class Default2 : System.Web.UI.Page
{
string connection = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlDataAdapter DA;
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bindgrid();
}
}
protected void BtnSave(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(connection);
con.Open();
SqlCommand cmd = new SqlCommand("insert into Category values('" + TextBox1.Text + "','" + TextBox2.Text + "')", con);
//Bindgrid();
cmd.ExecuteNonQuery();
con.Close();
Bindgrid();
}
public void Bindgrid()
{
SqlConnection con = new SqlConnection(connection);
con.Open();
DA = new SqlDataAdapter("Select * from Category", con);
DA.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
}
//protected void Button1_Click(object sender, EventArgs e)
//{
// SqlConnection con = new SqlConnection();
// con.Open();
// SqlCommand cmd = new SqlCommand("delete * from Category where Category='"+TextBox1.Text+"',Description='"+TextBox2.Text+"')",con);
// cmd.ExecuteNonQuery();
// con.Close();
//}
//protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
//{
// GridView1.EditIndex = e.NewEditIndex;
// Bindgrid();
//}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
TextBox textcategory = (TextBox)row.Cells[2].FindControl("TextBox4");
TextBox textDescription = (TextBox)row.Cells[3].FindControl("TextBox6");
SqlConnection con = new SqlConnection(connection);
con.Open();
SqlCommand cmd = new SqlCommand("update Category set Description='" + textDescription.Text + "' where categoryName='" + textcategory.Text + "'", con);
cmd.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
Bindgrid();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
Bindgrid();
}
protected void TextBox6_TextChanged(object sender, EventArgs e)
{
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
Bindgrid();
}
protected void Button7_Click(object sender, EventArgs e)
{
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
//TextBox textcategory = (TextBox)row.FindControl("TextBox4");
Label lbldeleteID = (Label)row.FindControl("Label1");
SqlConnection con = new SqlConnection(connection);
con.Open();
SqlCommand cmd = new SqlCommand("delete from Category where categoryName='" + lbldeleteID.Text + "'", con);
cmd.ExecuteNonQuery();
Bindgrid();
con.Close();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
TextBox txtcategoryName = (TextBox)GridView1.FooterRow.FindControl("TextBox8");
TextBox txtDescription = (TextBox)GridView1.FooterRow.FindControl("TextBox7");
//Label lblDescription = (Label)GridView1.FooterRow.FindControl("Label2");
SqlConnection con = new SqlConnection(connection);
con.Open();
SqlCommand cmd = new SqlCommand("insert into Category values ('"+txtcategoryName.Text+"','"+txtDescription.Text+"')",con );
cmd.ExecuteNonQuery();
con.Close();
Bindgrid();
}
}
do check the attachment for gridview reference
nice code friend