Aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="grid2.aspx.cs" Inherits="Finalgrid.grid2" %>
<!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">
<div>
<asp:GridView ID="gv1" runat="server" AutoGenerateColumns ="False"
onrowdatabound="gv1_RowDataBound" onrowdeleting="gv1_RowDeleting"
onrowediting="gv1_RowEditing" DataKeyNames="EID" ShowFooter="True"
onrowcommand="gv1_RowCommand" onrowupdating="gv1_RowUpdating"
BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px"
CellPadding="3" GridLines="None" CellSpacing="1"
onrowcancelingedit="gv1_RowCancelingEdit" >
<Columns >
<asp:TemplateField HeaderText="EID">
<ItemTemplate>
<%# Eval("EID") %>
</ItemTemplate>
<EditItemTemplate >
<asp:Label ID="lbleid" runat="server" Text='<%#Eval("EID") %>'></asp:Label>
</EditItemTemplate>
<FooterTemplate >
<asp:LinkButton ID="lnkinsert" runat="server" Text= "Insert" CommandName="Insert" ></asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EMPLOYEE NAME">
<ItemTemplate>
<%#Eval("ENAME") %>
</ItemTemplate>
<EditItemTemplate >
<asp:TextBox ID="txtename" runat="server" Text='<%#Eval("ENAME") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate >
<asp:TextBox ID="txtaddname" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DESIGNATION">
<ItemTemplate>
<%#Eval("DESIGNAME") %>
</ItemTemplate>
<EditItemTemplate >
<asp:DropDownList ID="ddldesignation" runat="server" width ="150px">
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate >
<asp:DropDownList ID="ddladddesig" runat="server" Width="150px">
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton ="true" ControlStyle-ForeColor ="Blue " >
<ControlStyle ForeColor="Blue"></ControlStyle>
</asp:CommandField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton CommandName ="Delete" CommandArgument ='<%#Eval("EID") %>' runat="server" ID ="lnkdelete" Text="Delete" ></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
<PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="#DEDFDE" ForeColor="Black" />
<SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#594B9C" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#33276A" />
</asp:GridView>
</div>
</form>
</body>
</html>
Aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
namespace Finalgrid
{
public partial class grid2 : System.Web.UI.Page
{
string sqlconn = ConfigurationManager.ConnectionStrings["connect"].ToString();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
public void BindGrid()
{
SqlConnection oconn = new SqlConnection(sqlconn);
oconn.Open();
SqlCommand ocmd = new SqlCommand("SELECT E.EID,E.ENAME,D.DESIGNAME FROM EMPLOYEE E INNER JOIN DESIGNATION D ON E.DESIGID=D.DESIGID", oconn);
SqlDataAdapter oda = new SqlDataAdapter(ocmd );
SqlCommandBuilder builder = new SqlCommandBuilder(oda);
DataSet ds = new DataSet();
oda.Fill(ds);
gv1.DataSource = ds;
gv1.DataBind();
}
protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
{
SqlConnection oconn = new SqlConnection(sqlconn);
oconn.Open();
SqlCommand ocmd = new SqlCommand("SELECT* FROM DESIGNATION", oconn);
SqlDataAdapter oda = new SqlDataAdapter(ocmd);
SqlCommandBuilder builder = new SqlCommandBuilder(oda);
DataSet ds = new DataSet();
oda.Fill(ds);
DropDownList ddl = (DropDownList)e.Row.FindControl("ddldesignation");
if (ddl != null)
{
ddl.DataSource = ds;
ddl.DataValueField = "DESIGID";
ddl.DataTextField = "DESIGNAME";
ddl.DataBind();
}
if (e.Row.RowType == DataControlRowType.Footer)
{
DropDownList ddldesig = (DropDownList)e.Row.FindControl("ddladddesig");
ddldesig.DataSource = ds;
ddldesig.DataValueField = "DESIGID";
ddldesig.DataTextField = "DESIGNAME";
ddldesig.DataBind();
}
}
protected void gv1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int EID = Convert.ToInt32(gv1.DataKeys[e.RowIndex].Value);
SqlConnection oconn = new SqlConnection(sqlconn);
oconn.Open();
SqlCommand ocmd = new SqlCommand();
ocmd.CommandText= "DELETE FROM EMPLOYEE WHERE EID=@EID";
ocmd.Parameters.AddWithValue("@EID", EID);
ocmd.Connection = oconn;
ocmd.ExecuteNonQuery();
oconn.Close();
BindGrid();
}
protected void gv1_RowEditing(object sender, GridViewEditEventArgs e)
{
gv1.EditIndex = e.NewEditIndex;
BindGrid();
}
protected void gv1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if(e.CommandName.Equals ("Insert"))
{
TextBox txtnewname = (TextBox)gv1.FooterRow.FindControl("txtaddname");
DropDownList ddlnew = (DropDownList)gv1.FooterRow.FindControl("ddladddesig");
SqlConnection oconn = new SqlConnection(sqlconn);
oconn.Open();
SqlCommand ocmd = new SqlCommand();
ocmd.CommandText = "INSERT INTO EMPLOYEE VALUES ('"+ txtnewname .Text +"'," + ddlnew.SelectedValue +")";
ocmd.Connection = oconn;
ocmd.ExecuteNonQuery();
BindGrid();
}
}
protected void gv1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int EID = int.Parse (((Label)(gv1.Rows[e.RowIndex].Cells[1].FindControl ("lbleid"))).Text);
string ENAME = ((TextBox)(gv1.Rows[e.RowIndex].Cells[1].FindControl("txtename"))).Text;
int DESIGID = int.Parse (((DropDownList )(gv1.Rows[e.RowIndex].Cells[1].FindControl ("ddldesignation"))).SelectedValue);
SqlConnection oconn = new SqlConnection(sqlconn);
oconn.Open();
SqlCommand ocmd = new SqlCommand();
ocmd.CommandText= "UPDATE EMPLOYEE SET ENAME=@ENAME ,DESIGID=@DESIGID WHERE EID=@EID ";
ocmd.Parameters.AddWithValue ("@EID",EID);
ocmd.Parameters.AddWithValue ("@ENAME",ENAME );
ocmd.Parameters.AddWithValue ("@DESIGID",DESIGID);
ocmd.Connection = oconn;
ocmd.ExecuteNonQuery();
gv1.EditIndex =-1;
BindGrid();
}
protected void gv1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gv1.EditIndex = -1;
BindGrid();
}
}
}