Monday, February 18, 2008

How to Insert,Edit,Update and Delete in GridView

<asp:GridView ID="GV" runat="server" Style="z-index: 100; left: 47px; position: absolute;
top: 162px" AutoGenerateColumns="False" OnRowCommand="GV_RowCommand" OnRowDeleting="GV_RowDeleting"
OnRowEditing="GV_RowEditing" OnRowDataBound="GV_RowDataBound" ShowFooter="True"
OnRowUpdating="GV_RowUpdating" OnRowCancelingEdit="GV_RowCancelingEdit">
<AlternatingRowStyle BackColor="aliceblue" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="TxtName" Text='<% # Bind("NAME") %>' runat="server" Style="z-index: 100;"></asp:Label>
<asp:Label ID="TxtSNO" Text='<% # Bind("SNO") %>' runat="server" Style="z-index: 100;
display: none;"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TxtName" Width="50" Text='<% # Bind("NAME") %>' runat="server" Style="z-index: 100;"></asp:TextBox>
<asp:TextBox ID="TxtSNO" Text='<% # Bind("SNO") %>' runat="server" Style="z-index: 100;
display: none;"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TxtNewName" Text='' runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="TxtNewName"
ErrorMessage="fdgdfgdfgd" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="TxtDept" Text='<% # Bind("DEPT") %>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TxtDept" Text='<% # Bind("DEPT") %>' runat="server"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TxtNewDept" Text='' runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="TxtDesig" Text='<%#Bind("DESIGNATION") %>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TxtDesig" Text='<%#Bind("DESIGNATION") %>' runat="server"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TxtNewDesig" Text='' runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="TxtSal" Text='<%#Bind("SALARY") %>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TxtSal" Text='<%#Bind("SALARY") %>' runat="server"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TxtNewSal" Text='' runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<FooterTemplate>
<asp:LinkButton ID="btnNew" runat="server" CommandName="Insert" Text="Insert" />
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>



//Server Code
//=============
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class GridView : System.Web.UI.Page
{
SqlDataAdapter adp = new SqlDataAdapter();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection();

protected void Page_Load(object sender, EventArgs e)
{
con = new SqlConnection("Uid=sa;Pwd=sa;server=IISSERVER;database=Kanna;");
if(!IsPostBack)
Rebind();
}
protected void GV_RowEditing(object sender, GridViewEditEventArgs e)
{
GV.EditIndex = e.NewEditIndex;
Rebind();
}
protected void GV_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
Label id = (Label)GV.Rows[e.RowIndex].Cells[0].FindControl("TxtSNO");
adp = new SqlDataAdapter("delete from sample where sno=" + id.Text, con);
adp.Fill(dt);
GV.ShowFooter = true;
Rebind();
}

protected void GV_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("Insert"))
{
TextBox name = (TextBox)GV.FooterRow.FindControl("TxtNewName");
TextBox dept = (TextBox)GV.FooterRow.FindControl("TxtNewDept");
TextBox desig = (TextBox)GV.FooterRow.FindControl("TxtNewDesig");
TextBox sal = (TextBox)GV.FooterRow.FindControl("TxtNewSal");
if (name.Text != "" && dept.Text != "" && desig.Text != "" && sal.Text != "")
{
adp = new SqlDataAdapter("insert into sample values('" + name.Text + "','" + dept.Text + "','" + desig.Text + "'," + Convert.ToInt32(sal.Text) + ")", con);
adp.Fill(dt);
}
Rebind();
}

if (e.CommandName == "Edit")
{
GV.EditIndex = 0;
GV.ShowFooter = false;
Console.WriteLine("Edited");
}
}

protected void GV_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex != -1)
{

e.Row.Attributes.Add("OnMouseOver", "this.style.backgroundColor='aliceblue'");

if (e.Row.RowIndex % 2 == 0)
e.Row.Attributes.Add("OnMouseOut", "this.style.backgroundColor='transparent'");
else
e.Row.Attributes.Add("OnMouseOver", "this.style.backgroundColor='aliceblue'");
}
}

public void Rebind()
{
adp = new SqlDataAdapter("select * from sample", con);
adp.Fill(dt);

if (dt.Rows.Count > 0)
{
GV.DataSource = dt;
GV.DataBind();
}
else
{
dt.Rows.Add(dt.NewRow());
GV.DataSource = dt;
GV.DataBind();

int TotalColumns = GV.Rows[0].Cells.Count;
GV.Rows[0].Cells.Clear();
GV.Rows[0].Cells.Add(new TableCell());
GV.Rows[0].Cells[0].ColumnSpan = TotalColumns;
}


}

protected void GV_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox id =(TextBox)GV.Rows[e.RowIndex].Cells[0].FindControl("TxtSNO");
TextBox name =(TextBox)GV.Rows[e.RowIndex].Cells[0].FindControl("TxtName");
TextBox dept =(TextBox)GV.Rows[e.RowIndex].Cells[1].FindControl("TxtDept");
TextBox desig =(TextBox)GV.Rows[e.RowIndex].Cells[2].FindControl("TxtDesig");
TextBox sal =(TextBox)GV.Rows[e.RowIndex].Cells[3].FindControl("TxtSal");

adp = new SqlDataAdapter("update sample set name='"+name.Text+"',dept='"+dept.Text+"',designation='"+desig.Text+"',salary="+Convert.ToInt32(sal.Text)+" where sno="+Convert.ToInt32(id.Text),con);
adp.Fill(dt);
GV.EditIndex = -1;
GV.ShowFooter = true;
Rebind();
}
protected void GV_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GV.EditIndex = -1;
GV.ShowFooter = true;
Rebind();
}
}

No comments:

 
Feedback Form