This code demonstrate the basic gridview operation like load data into gridview, Sort, Update using gridview etc.
using
System;
using
System.Data;
using
System.Configuration;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
using
System.Data.SqlClient;
public
partial class _Default : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox t1, t2, t3;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
loaddata(
"");
}
}
public void loaddata(string q)
{
if (q == "")
{
SqlConnection con =
new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
con.Open();
string qry = "Select * from ShopCart";
SqlDataAdapter ad =
new SqlDataAdapter(qry, con);
DataSet ds =
new DataSet();
ad.Fill(ds);
if (ds.Tables[0].Rows.Count != 0)
{
this.gvedit.DataSource = ds.Tables[0].DefaultView;
this.gvedit.DataBind();
}
}
else
{
SqlConnection con =
new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
con.Open();
SqlDataAdapter ad =
new SqlDataAdapter(q, con);
DataSet ds =
new DataSet();
ad.Fill(ds);
if (ds.Tables[0].Rows.Count != 0)
{
this.gvedit.DataSource = ds.Tables[0].DefaultView;
this.gvedit.DataBind();
}
}
}
protected void gvedit_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvedit.PageIndex = e.NewPageIndex;
loaddata(
"");
}
protected void gvedit_RowEditing(object sender, GridViewEditEventArgs e)
{
gvedit.EditIndex = e.NewEditIndex;
loaddata(
"");
}
protected void gvedit_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//string id = gvedit.DataKeys[e.RowIndex].Values[e.RowIndex].ToString();
GridViewRow r = gvedit.Rows[e.RowIndex];
int rindex = r.RowIndex;
DataKey dk = gvedit.DataKeys[rindex];
string id = Convert.ToString(dk.Value);
if (r != null)
{
TextBox t1= (TextBox)gvedit.Rows[e.RowIndex].FindControl(
"TextBox2") as TextBox;
TextBox t2 = (TextBox)gvedit.Rows[e.RowIndex].FindControl(
"TextBox3") as TextBox;
TextBox t3 = (TextBox)gvedit.Rows[e.RowIndex].FindControl(
"TextBox4") as TextBox;
SqlConnection con =
new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
con.Open();
string qry = "update ShopCart set BookTitle='" + t1.Text + "',BookPrice='" + t2.Text + "',BookQty='" + t3.Text+ "' where ID='" + id + "'";
SqlCommand cmd =
new SqlCommand(qry, con);
cmd.ExecuteNonQuery();
con.Close();
loaddata(
"");
}
}
protected void gvedit_Sorting(object sender, GridViewSortEventArgs e)
{
string exp=e.SortExpression.ToString();
string qry = "select * from ShopCart order by " + exp + " asc";
loaddata(qry);
}
protected void gvedit_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridViewRow r = gvedit.Rows[e.RowIndex];
int rindex = r.RowIndex;
DataKey dk = gvedit.DataKeys[rindex];
string id = Convert.ToString(dk.Value);
//string id = gvedit.DataKeys[e.RowIndex].Values[e.RowIndex].ToString();
SqlConnection con =
new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
con.Open();
string qry = "delete from ShopCart where ID='" + id + "'";
SqlCommand cmd =
new SqlCommand(qry, con);
cmd.ExecuteNonQuery();
con.Close();
loaddata(
"");
}
protected void gvedit_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gvedit.EditIndex = -1;
loaddata(
"");
}
protected void gvedit_RowCommand(object sender, GridViewCommandEventArgs e)
{
}
}