Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

[Managing GridView With Templete Field] [2009]

Screen Design:

DataBase:

.ASPX
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridTemp.aspx.cs" Inherits="databaseapps.GridTemp" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <center> <div> <asp:GridView ID="GridEmp" runat="server" AutoGenerateColumns="False"

M SUDANARAO PATIBANDLA

[Managing GridView With Templete Field] [2009]

DataKeyNames="EmpId" onrowcancelingedit="GridEmp_RowCancelingEdit" onrowdatabound="GridEmp_RowDataBound" onrowdeleting="GridEmp_RowDeleting" onrowediting="GridEmp_RowEditing" onrowupdating="GridEmp_RowUpdating"> <Columns> <asp:BoundField HeaderText="Employee" DataField="EmpName" /> <asp:TemplateField HeaderText="Dept"> <ItemTemplate><asp:Label ID="lblgdept" runat="server"><%#Eval("DeptName")%></asp:Label></ItemTemplate> <EditItemTemplate><asp:DropDownList ID="DDLGridDept" runat="server"></asp:DropDownList></EditItemTemplate> </asp:TemplateField> <asp:BoundField DataField="EmpSal" HeaderText="Salary" /> <asp:BoundField DataField="EmpAddress" HeaderText="Address" /> <asp:CommandField ShowEditButton="True" /> <asp:CommandField ShowDeleteButton="True" /> </Columns> </asp:GridView> </div> </center> </form> </body> </html>

.ASPX.CS using using using using using using using using System; System.Data; System.Collections.Generic; System.Linq; System.Web; System.Web.UI; System.Web.UI.WebControls; System.Data.SqlClient;

namespace databaseapps { public partial class GridTemp : System.Web.UI.Page { public string con = "Data Source=MSRAO-PC;Initial Catalog=sample;User Id=sa;Password=Msrao@1983"; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { LoadData(); } } private void LoadData() { SqlConnection cn = new SqlConnection(); cn.ConnectionString = con; cn.Open();

M SUDANARAO PATIBANDLA

[Managing GridView With Templete Field] [2009]

DataSet ds = new DataSet(); SqlDataAdapter da = new SqlDataAdapter("select * from tbl_Emp A inner join tbl_Dept B on A.DeptId=B.DeptId", cn); da.Fill(ds); GridEmp.DataSource = ds.Tables[0]; GridEmp.DataBind(); } protected void GridEmp_RowEditing(object sender, GridViewEditEventArgs e) { GridEmp.EditIndex = e.NewEditIndex; LoadData(); } protected void GridEmp_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridEmp.EditIndex = -1; LoadData(); } protected void GridEmp_RowUpdating(object sender, GridViewUpdateEventArgs e) { try { int key = int.Parse(GridEmp.DataKeys[e.RowIndex].Value.ToString()); SqlConnection cn = new SqlConnection(); cn.ConnectionString = con; cn.Open(); System.Web.UI.WebControls.TextBox Empname = (System.Web.UI.WebControls.TextBox)GridEmp.Rows[e.RowIndex].Cells[0].Controls[0]; DropDownList DDL = (DropDownList)GridEmp.Rows[e.RowIndex].Cells[1].Controls[0]; string DeptID = DDL.SelectedValue.ToString(); System.Web.UI.WebControls.TextBox empsal = (System.Web.UI.WebControls.TextBox)GridEmp.Rows[e.RowIndex].Cells[2].Controls[0]; System.Web.UI.WebControls.TextBox empaddress = (System.Web.UI.WebControls.TextBox)GridEmp.Rows[e.RowIndex].Cells[3].Controls[0]; string string string string ename = Empname.Text; dId = DeptID; esal = empsal.Text; eaddress = empaddress.Text;

SqlCommand cmd = new SqlCommand("update tbl_Emp set EmpName='" + ename + "',DeptId='" + int.Parse(dId) + "',EmpSal='" + Convert.ToDecimal(esal) + "',EmpAddress='" + eaddress + "' where EmpId='" + key.ToString() + "'", cn); cmd.ExecuteNonQuery(); GridEmp.EditIndex = -1; LoadData(); }

M SUDANARAO PATIBANDLA

[Managing GridView With Templete Field] [2009]

catch { } } protected void GridEmp_RowDeleting(object sender, GridViewDeleteEventArgs e) { int key = int.Parse(GridEmp.DataKeys[e.RowIndex].Value.ToString()); SqlConnection cn = new SqlConnection(); cn.ConnectionString = con; cn.Open(); SqlCommand cmd = new SqlCommand("delete from tbl_Emp where EmpId='" + key.ToString() + "'", cn); cmd.ExecuteNonQuery(); LoadData(); } protected void GridEmp_RowDataBound(object sender, GridViewRowEventArgs e) { if ((e.Row.RowState & DataControlRowState.Edit) > 0) { SqlConnection cn = new SqlConnection(); cn.ConnectionString = con; cn.Open(); DropDownList DDL = (DropDownList)e.Row.Cells[1].Controls[0]; DataSet ds = new DataSet(); SqlDataAdapter da = new SqlDataAdapter("select * from tbl_Dept", cn); da.Fill(ds); DDL.DataSource = ds.Tables[0]; DDL.DataTextField = "DeptName"; DDL.DataValueField = "DeptId"; DDL.DataBind(); DataRowView DRV = (DataRowView)e.Row.DataItem; string DeptName = DRV["DeptName"].ToString(); DDL.Items.FindByText(DeptName).Selected = true; cn.Close(); } } } }

M SUDANARAO PATIBANDLA

You might also like