<head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <table align="center"><tr><td> <asp:GridView ID="gv1" runat="server" AutoGenerateColumns="False" AutoGenerateDeleteButton="True" BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" CellPadding="2" GridLines="None" onrowdeleting="gv1_RowDeleting" ForeColor="Black" AutoGenerateEditButton="True" onrowcancelingedit="gv1_RowCancelingEdit" onrowediting="gv1_RowEditing" onrowupdating="gv1_RowUpdating"> <Columns> <asp:BoundField HeaderText="Dept Id" DataField="DeptId"/> <asp:BoundField HeaderText="Dept Name" DataField="DepartmentName"/> </Columns> <FooterStyle BackColor="Tan" /> <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" /> <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" /> <HeaderStyle BackColor="Tan" Font-Bold="True" /> <AlternatingRowStyle BackColor="PaleGoldenrod" /> </asp:GridView></td></tr></table> <table align="center" bgcolor="#ff9900"><tr><td><asp:Label ID="lblDEptId" Text="Dept Id" runat="server"></asp:Label> </td> <td><asp:TextBox ID="txtlblDEptId" runat="server"></asp:TextBox></td></tr> <tr><td><asp:Label ID="LblDeptName" Text="Dept Name" runat="server"></asp:Label> </td> <td><asp:TextBox ID="TxtDeptName" runat="server"></asp:TextBox></td></tr> </table> <table align="center"><tr><td><asp:Button ID="btnsave" Text="Save" runat="server" onclick="btnsave_Click" /></td> <td><asp:Button ID="Btncancel" Text="Cancel" runat="server" onclick="Btncancel_Click" /></td></tr></table> </div> </form> </body> </html>
protected void Page_Load(object sender, EventArgs e) { if (Page.IsPostBack == false) { GetData(); } } private void GetData() { SqlDataAdapter da = new SqlDataAdapter("select * from Department", con); DataSet ds = new DataSet(); da.Fill(ds, "Department"); gv1.DataSource = ds.Tables[0]; gv1.DataBind(); } protected void btnsave_Click(object sender, EventArgs e) { string s = "insert into Department values('" + txtlblDEptId.Text + "','" + TxtDeptName.Text + "')"; SqlCommand cmd = new SqlCommand(s, con); cmd.CommandType = CommandType.Text; con.Open(); int i = cmd.ExecuteNonQuery(); con.Close(); GetData(); } protected void gv1_RowDeleting(object sender, GridViewDeleteEventArgs e) { Response.Write(e.RowIndex); string s = gv1.Rows[e.RowIndex].Cells[1].Text; Response.Write(s); da = new SqlDataAdapter("delete from Department where DeptId=" + s, con); ds = new DataSet(); da.Fill(ds, "Department"); GetData(); } protected void Btncancel_Click(object sender, EventArgs e) { txtlblDEptId.Text = ""; TxtDeptName.Text = ""; } protected void gv1_RowEditing(object sender, GridViewEditEventArgs e) { gv1.EditIndex = e.NewEditIndex; GetData(); } protected void gv1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { gv1.EditIndex = -1; GetData(); } protected void gv1_RowUpdating(object sender, GridViewUpdateEventArgs e) { GridViewRow r = gv1.Rows[e.RowIndex]; int mdeptid; string mdeptname; TextBox t1 = (TextBox)r.Cells[1].Controls[0]; mdeptid = Convert.ToInt32(t1.Text); t1 = (TextBox)r.Cells[2].Controls[0]; mdeptname = (t1.Text); string s = "update Department set DepartmentName='" + mdeptname + "' where DeptId=" + mdeptid; SqlCommand cmd = new SqlCommand(s, con); cmd.CommandType = CommandType.Text; con.Open(); int i = cmd.ExecuteNonQuery(); con.Close(); gv1.EditIndex = -1; GetData(); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)