Here
first I created a table name 'emp' in my database.it contains 3 colomns
as id,name,marks,I am creating this colomns in gridview as follows:
01 | < asp:GridView ID = "GridView1" runat = "server" AutoGenerateColumns = "False" Style = "z-index: 100;left: 298px; position: absolute; top: 118px" > |
03 | < asp:TemplateField HeaderText = "IDNO" > |
05 | < asp:LabelID = "lblid" runat = "server" Text='<%#Eval("rowid")%>'></ asp:Labe > |
08 | < asp:TemplateField HeaderText = "Name" > |
09 | < ItemTemplate > <%#Eval("name") %></ ItemTemplate > |
11 | < asp:TemplateField HeaderText = "Marks" > |
12 | < ItemTemplate ><%#Eval("marks") %></ ItemTemplate > |
In default.asp.cs page fill the gridview using SqlDataAdapter using below code:
1 | SqlDataAdapter da = new SqlDataAdapter( "select * from emp" , conn); |
2 | DataSet ds = new DataSet(); |
4 | GridView1.DataSource = ds.Tables[0]; |
Next in Default.asp page select your gridview in Properties set AutoGenarateDeleteButton=True and AutoGenarateEditButton=True.
Next we have to write a code for editing,updating,cancel:In Default.aspx source code we have to add <EditItemTemplate>
This <EditItemTemplate> is used to Edit the Row in Gridview.Here I am going to Edit only two columns name and marks.
For Editing a Gridview:
In Gridview Events:Double Click on RowEditing Event and write below code
1 | protected void GridView1_RowEditing( object sender,GridViewEditEventArgs e) |
3 | GridView1.EditIndex = e.NewEditIndex; |
When you click on Edit link it shows Update,Cancel links
For Updating a Gridview:
Updating link is used to update a Particular row in emp table using Gridview.
Double click on RowUpdating Event and write below code
01 | protected void GridView1_RowUpdating( object sender, GridViewUpdateEventArgs e) |
03 | GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex]; |
04 | Label lbl = (Label)row.FindControl( "lblid" ); |
05 | TextBox textname = (TextBox)row.FindControl( "textbox1" ); |
06 | TextBox textmarks = (TextBox)row.FindControl( "textbox2" ); |
08 | GridView1.EditIndex = -1; |
10 | SqlCommand cmd = new SqlCommand( "update emp set marks=" + textmarks.Text + " , name='" + textname.Text + "' where rowid=" + lbl.Text + "" , conn); |
12 | cmd.ExecuteNonQuery(); |
For Canceling a gridview row Operation:
Cancel Link in used to cancel the particular row operation before upadating.when you click on Gricview it goes in first stage.
Double click on RowCancelingEdit Event and wrtie belwo code
1 | protected void GridView1_RowCancelingEdit( object sender,GridViewCancelEditEventArgs e) |
3 | GridView1.EditIndex = -1; |
For Deleting a Gridview row:
Delete Link is used to delete a Row in a emp table.it permanently deletes a particular Row From GridView
Double Click on RowDeleting Event and write below code
01 | protected void GridView1_RowDeleting( object sender, GridViewDeleteEventArgs e) |
03 | GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex]; |
04 | Label lbldeleteID = (Label)row.FindControl( "lblid" ); |
06 | SqlCommand cmd = new SqlCommand( "delete emp where rowid=" + lbldeleteID.Text + "" , conn); |
07 | cmd.ExecuteNonQuery(); |
The Complete code is written as follows:
01 | <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> |
07 | < title >Untitled Page</ title > |
10 | < form id = "form1" runat = "server" > |
12 | < asp:GridView ID = "GridView1" runat = "server" AutoGenerateColumns = "False" Style="z-index: 100; |
13 | left: 298px; position: absolute; top: 118px" AllowSorting = "True" AutoGenerateDeleteButton = "True" AutoGenerateEditButton = "True" OnRowEditing = "GridView1_RowEditing" OnRowCancelingEdit = "GridView1_RowCancelingEdit" OnRowUpdating = "GridView1_RowUpdating" OnRowDeleting = "GridView1_RowDeleting" BackColor = "#FF8080" BorderColor = "SaddleBrown" BorderStyle = "None" CellPadding = "4" ForeColor = "#333333" GridLines = "None" PageSize = "5" > |
15 | < asp:TemplateField HeaderText = "IDNO" >< ItemTemplate >< asp:Label ID = "lblid" runat = "server" Text ='<%#Eval("rowid") %>' ></ asp:Label ></ ItemTemplate ></ asp:TemplateField > |
17 | < asp:TemplateField HeaderText = "Name" >< ItemTemplate > <%#Eval("name") %></ ItemTemplate > |
19 | < asp:TextBox ID = "textbox1" runat = "server" Text ='<%#Eval("name") %>'></ asp:TextBox > |
23 | < asp:TemplateField HeaderText = "Marks" >< ItemTemplate ><%#Eval("marks") %> </ ItemTemplate > |
25 | < asp:TextBox ID = "textbox2" runat = "server" Text ='<%#Eval("marks") %>'></ asp:TextBox > |
30 | < FooterStyle BackColor = "#990000" Font-Bold = "True" ForeColor = "White" /> |
31 | < RowStyle BackColor = "#FFFBD6" ForeColor = "#333333" /> |
32 | < SelectedRowStyle BackColor = "#FFCC66" Font-Bold = "True" ForeColor = "Navy" /> |
33 | < PagerStyle BackColor = "#FFCC66" ForeColor = "#333333" HorizontalAlign = "Center" /> |
34 | < HeaderStyle BackColor = "#990000" Font-Bold = "True" ForeColor = "White" /> |
35 | < AlternatingRowStyle BackColor = "White" /> |
and in your .cs page
03 | using System.Configuration; |
05 | using System.Web.Security; |
07 | using System.Web.UI.WebControls; |
08 | using System.Web.UI.WebControls.WebParts; |
09 | using System.Web.UI.HtmlControls; |
10 | using System.Data.SqlClient; |
11 | public partial class _Default : System.Web.UI.Page |
14 | protected void Page_Load( object sender, EventArgs e) |
16 | conn = new SqlConnection( "Data Source=INTHIYAAZ;Initial Catalog=shakeer;uid=sa;pwd=sa;" ); |
22 | protected void GridView1_RowEditing( object sender, GridViewEditEventArgs e) |
24 | GridView1.EditIndex = e.NewEditIndex; |
28 | protected void GridView1_RowCancelingEdit( object sender, GridViewCancelEditEventArgs e) |
30 | GridView1.EditIndex = -1; |
34 | protected void GridView1_RowUpdating( object sender, GridViewUpdateEventArgs e) |
36 | GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex]; |
37 | Label lbl = (Label)row.FindControl( "lblid" ); |
38 | TextBox textname = (TextBox)row.FindControl( "textbox1" ); |
39 | TextBox textmarks = (TextBox)row.FindControl( "textbox2" ); |
41 | GridView1.EditIndex = -1; |
43 | SqlCommand cmd = new SqlCommand( "update emp set marks=" + textmarks.Text + " , name='" + textname.Text + "' where rowid=" + lbl.Text + "" , conn); |
45 | cmd.ExecuteNonQuery(); |
54 | SqlDataAdapter da = new SqlDataAdapter( "select * from emp" , conn); |
55 | DataSet ds = new DataSet(); |
57 | GridView1.DataSource = ds.Tables[0]; |
61 | protected void GridView1_SelectedIndexChanging( object sender, GridViewSelectEventArgs e) |
65 | protected void GridView1_RowCommand( object sender, GridViewCommandEventArgs e) |
69 | protected void GridView1_RowDeleting( object sender, GridViewDeleteEventArgs e) |
71 | GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex]; |
72 | Label lbldeleteID = (Label)row.FindControl( "lblid" ); |
74 | SqlCommand cmd = new SqlCommand( "delete emp where rowid=" + lbldeleteID.Text + "" , conn); |
75 | cmd.ExecuteNonQuery(); |
79 | protected void GridView1_PageIndexChanging( object sender, GridViewPageEventArgs e) |
81 | GridView1.PageIndex = e.NewPageIndex; |
No comments:
Post a Comment