.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> ASP.NET Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Gridview operation in ASP.NET

Posted By:Athira Sreejith       Posted Date: March 24, 2009    Points: 10    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  
 

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)

{

}

}



     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend