http://dbakings.com/ASP/ASPGridViewEditUpdateDelete.aspx
ASP.NET Gridview is a data control used to display data in tabular format. By default page size is 10. Examplecreate a database named asp.create database asp create a table named student in asp database create table student (sno int,sname varchar(20),course varchar(20)) insert more than 10 rows of data into it. insert into student values(1,’sudhir’,'dotnet’) insert into student values(2,’sri’,'java’) insert into student values(3,’venki’,'php’) now go to visual studio. create new empty website. add a webform (Default.aspx). drag and drop ASP.NET Gridview control from toolbar under data tab to your design page. after adding a ASP.NET Gridview control. go to code page. Default.aspx.cs page. write a function which fills ASP.NET Gridview with the student table data. and add it in page load event. ASP.NET Source codeusing 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.Data; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { getdata(); } } DataSet ds = new DataSet(); SqlConnection con = new SqlConnection(“Data Source=.;Initial Catalog=asp;User ID=sa;Password=vrs”); public void getdata() { SqlCommand cmd = new SqlCommand(“select * from student”, con); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(ds,”student”); GridView1.DataSource = ds.Tables["student"]; GridView1.DataBind(); } } Output:on page load when the page is requested for the first time gridview is filled with student data. grid view paging:now go to design view of the page. change the ASP.NET Gridview property allow paging to true. now paging property is enabled for the ASP.NET Gridview . follow the below steps for paging to work. select ASP.NET Gridview – properties – events – double click on pageindex changing. an event is generated in code page. this event will get fired when page of ASP.NET Gridview is changed. protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; getdata(); } e.New page index will have the page no. selected by the user. and page index of gridview is overrided with the new page index. Row editingASP.NET Gridview – properties – autogenerate edit button =trueedit buttons will be displayed on the gridview. now generate row editing event as below ASP.NET Gridview – properties – events – row editing write the following code in that row editing event.protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e){ GridView1.EditIndex = e.NewEditIndex; getdata(); } by default editindex = -1. when u click on edit button update and cancel are genereted . update is used to update the data after editing and cancel to bring back the ASP.NET Gridview to normal state from editing state. Now row cancelling event:ASP.NET Gridview– properties – events - RowCancelingEditwrite the following code in rowcanelling edit event which is fired when you click on cancel buttonprotected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e){ GridView1.EditIndex = -1; getdata(); } row updatingnow generate the row updating event which is fired when the user clicks on update button after finished editing.ASP.NET Gridview – properties – events – row updating write the below code in row updating event.Take 3 variables to store values we get from the textboxes to updatedeclare an object of type texbox row number is stored in e.rowindex cells index starts from 1 and control[0] means textbox Row updating Codeprotected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e){ string sno, sname, course; TextBox t = new TextBox(); sno = ((TextBox )GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text ; sname = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text; course = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text; SqlCommand cmd = new SqlCommand(“update student set sno=” + sno + “,sname=’” + sname + “‘,course=’” + course + “‘ where sno=” + sno + “”, con); con.Open(); cmd.ExecuteNonQuery(); con.Close(); GridView1.EditIndex = -1; getdata(); } Row DeletingASP.NET Gridview – properties -auto generate delete button = trueASP.NET Gridview – properties – events – row deleting write the following code to delete a row from the ASP.NET Gridview: take a variable sno to store value from textbox based on which the row is deleted protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { string sno; sno = GridView1.Rows[e.RowIndex].Cells[1].Text; SqlCommand cmd = new SqlCommand(“delete from student where sno=” + sno + “”, con); con.Open(); int i = cmd.ExecuteNonQuery(); if (i > 0) { Label1.Text = “successfully deleted”; } con.Close(); getdata(); } Sorting:ASP.NET Gridview -properties – allow sorting = trueASP.NET Gridview -properties – events – sorting write the below code for sorting: variable col will store the column name on which the user clicks dir will store direction which is again changed according to sql keyword protected void GridView1_Sorting(object sender, GridViewSortEventArgs e) { string col = e.SortExpression; string dir = e.SortDirection.ToString (); if(dir==”Ascending”) {dir=”asc”;} else {dir=”desc”;} SqlCommand cmd = new SqlCommand(“select * from student order by ” + col + ” ” + dir + “”, con); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds, “student”); GridView1.DataSource = ds.Tables["student"]; GridView1.DataBind(); } |
No comments:
Post a Comment