Thursday, March 8, 2007

Inserting,Deleting,Updating,Paging With Previous & Number Mode, Manual Paging All Through DataGrid

          This a simple program to insert,delete,update,Navigate Pages,Manually navigate pages in DataGrid

CodeBehind

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace TemplateGrid
{
///


/// Summary description for WebForm1.
///

public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Add;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected DataTable table = new DataTable();
string si;
string is1,is2,is3;
protected System.Web.UI.WebControls.CheckBox chk1;
protected System.Web.UI.WebControls.TextBox TextBox4;
protected System.Web.UI.WebControls.Button Button1;
SqlConnection con = new SqlConnection(@"Server=servername;DataBase=databasename;uid=sa;pwd=sa;");
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if(!IsPostBack)
{
FillDG();
BindDG();
}
DataGrid1.Columns[3].Visible=true;
DataGrid1.Columns[4].Visible =false;
DataGrid1.Columns[0].Visible=false;
DataGrid1.Columns[1].Visible=false;
}
public void FillDG()
{
SqlDataAdapter da = new SqlDataAdapter("Select * from Emp order by iEid",con);
da.Fill(table);
}
private void InitializeComponent()
{
this.DataGrid1.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_ItemCommand);
this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
this.chk1.CheckedChanged += new System.EventHandler(this.chk1_CheckedChanged);
this.Load += new System.EventHandler(this.Page_Load);
}
public void BindDG()
{
DataGrid1.DataSource=table;
DataGrid1.DataBind();
}
public void Nrow()
{
table.Rows.InsertAt(table.NewRow(),0);
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///


#endregion
private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
{}
private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = -1;
switch(e.CommandName)
{
case "Add":
DataGrid1.CurrentPageIndex = 0;
DataGrid1.Columns[0].Visible=true;
DataGrid1.Columns[1].Visible=true;
DataGrid1.Columns[2].Visible=false;
DataGrid1.Columns[3].Visible=false;
FillDG();
Nrow();
BindDG();
break;
case "Insert":
SqlDataAdapter ida = new SqlDataAdapter("SELECT MAX(iEid)+1 FROM Emp", con);
DataSet ids = new DataSet();
ida.Fill(ids, "O");
si=Convert.ToString(ids.Tables["O"].Rows[0].ItemArray[0]);
TextBox tEid = new TextBox();
tEid.Text =si;
is1=tEid.Text ;
TextBox tEname=(TextBox)e.Item.Cells[5].FindControl("TextBox2");
is2=tEname.Text;
TextBox tEsal=(TextBox)e.Item.Cells[6].FindControl("TextBox3");
is3=tEsal.Text;
SqlCommand icmd =new SqlCommand("insert into Emp(iEid,cEName,dSalary) values('" + is1+ "','" + tEname.Text +"','" + tEsal.Text + "')",con);
con.Open();
icmd.ExecuteNonQuery();
FillDG();
BindDG();
DataGrid1.Columns[0].Visible=false;
DataGrid1.Columns[1].Visible=false;
DataGrid1.Columns[2].Visible=true;
DataGrid1.Columns[3].Visible=true;
break;
case "Update":
string s1,s2,s3;
TextBox Eid=(TextBox)e.Item.Cells[4].FindControl("TextBox1");
s1=Eid.Text;
TextBox Ename=(TextBox)e.Item.Cells[5].FindControl("TextBox2");
s2=Ename.Text;
TextBox Esal=(TextBox)e.Item.Cells[6].FindControl("TextBox3");
s3=Esal.Text;
SqlCommand cmd =new SqlCommand("Update Emp Set dSalary=" + s3+ " ,cEName ='" + s2 + "' WHERE iEid=" + Convert.ToInt32(s1),con);
con.Open();
cmd.ExecuteNonQuery();
FillDG();
BindDG();
Response.Write("Update");
break;
case "Edit":
DataGrid1.EditItemIndex=e.Item.ItemIndex;
FillDG();
BindDG();
break;
case "Delete":
string s11;
s11=Convert.ToString(e.Item.Cells[4].Text);
Response.Write(s11);
TextBox iEid=(TextBox)e.Item.Cells[5].FindControl("TextBox2");
s11=iEid.Text;
SqlCommand Dcmd =new SqlCommand("Delete from emp where cEName= '"+ s11 +"' ",con);
con.Open();
Dcmd.ExecuteNonQuery();
FillDG();
BindDG();

break;
case "Cancel":
DataGrid1.Columns[2].Visible=true;
DataGrid1.EditItemIndex=-1;
FillDG();
BindDG();
break;
case "GoTo":
TextBox GT=(TextBox)e.Item.Cells[5].FindControl("TextBox5");
string GTs=GT.Text;
DataGrid1.CurrentPageIndex =Convert.ToInt16(GTs);
FillDG();
BindDG();
break;
}
}
private void chk1_CheckedChanged(object sender, System.EventArgs e)
{
if (chk1.Checked)
{
DataGrid1.PagerStyle.Mode=PagerMode.NumericPages;
chk1.Text="Changed To NextPrev";
DataGrid1.Columns[2].Visible=true;
FillDG();
BindDG();
}
else
{
DataGrid1.PagerStyle.Mode=PagerMode.NextPrev;
chk1.Text="NumericPages";
DataGrid1.Columns[2].Visible=true;
FillDG();
BindDG();
}
}
private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex=e.NewPageIndex;
FillDG();
BindDG();
}
}
}


HTML Part

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="TemplateGrid.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:datagrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 48px; POSITION: absolute; TOP: 40px" runat="server"
AutoGenerateColumns="False" Width="483px" BorderColor="#999999" BorderStyle="None" BorderWidth="1px"
BackColor="White" CellPadding="3" GridLines="Vertical" PageSize="3" AllowPaging="True" AllowSorting="True"
ShowFooter="True">
<SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#008A8C"></SelectedItemStyle>
<AlternatingItemStyle BackColor="Gainsboro"></AlternatingItemStyle>
<ItemStyle ForeColor="Black" BackColor="#EEEEEE"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#000084"></HeaderStyle>
<FooterStyle ForeColor="Black" BackColor="#CCCCCC"></FooterStyle>
<Columns>
<asp:ButtonColumn Text="Insert" CommandName="Insert"></asp:ButtonColumn>
<asp:ButtonColumn Text="Cancel" CommandName="Cancel"></asp:ButtonColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
<asp:ButtonColumn Text="Delete" CommandName="Delete"></asp:ButtonColumn>
<asp:TemplateColumn>
<HeaderTemplate>
Emp_Id
</HeaderTemplate>
<ItemTemplate>
<asp:TextBox id=TextBox1 runat="server" Width="64px" Text='<%# DataBinder.Eval(Container.DataItem, "iEid") %>'>
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<HeaderTemplate>
Employee_Name
</HeaderTemplate>
<ItemTemplate>
<asp:TextBox id=TextBox2 runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "cEName") %>' MaxLength="25">
</asp:TextBox>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox id="TextBox5" runat="server" Width="56px"></asp:TextBox>
<asp:Button id="btGoTo" runat="server" Text="GoTo" Font-Bold="True" Font-Names="Times New Roman"
CommandName="GoTo"></asp:Button>
</FooterTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<HeaderTemplate>
Employee Salary
</HeaderTemplate>
<ItemTemplate>
<asp:TextBox id=TextBox3 runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "dSalary") %>' MaxLength="7">
</asp:TextBox>
</ItemTemplate>
<FooterTemplate>
<asp:Button id="Add" runat="server" Text="Add" CommandName="Add"></asp:Button>
</FooterTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle NextPageText="Next »" PrevPageText="« Prev" HorizontalAlign="Center" ForeColor="Black"
BackColor="#999999"></PagerStyle>
</asp:datagrid>
<p><asp:Checkbox id="chk1" runat="server" Text="NumericPages" Font-Name="Verdana" Font-Size="8pt"
AutoPostBack="true" style="Z-INDEX: 102; LEFT: 8px; POSITION: absolute; TOP: 16px" />
</p>
</form>
</body>
</HTML>



Table Struture
Table Name = Emp



Intial Screen

Paging Style
when the Checked Box is Checked then Paging Mode will changed to Number Mode If Unchecked it will be in Previous&Next Mode

Manually Changing The Page
You can manually navigate Pages Through Passing the needed page number in the TextBox and Click GoTo Button,It will display the Corresponding Page,The Page Index Starts Form 0 to n,

Inserting A Row

You can insert a record through DataGrid,When You Clicked the Add Button new DataTable will inserted,


No comments:

Creative Commons License
Disclaimers:We have tried hard to provide accurate information, as a user, you agree that you bear sole responsibility for your own decisions to use any programs, documents, source code, tips, articles or any other information provided on this Blog.