Thursday, March 22, 2007

Create Row Dynamically in DataGrid

HTML Part

< %@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="TestDGrid.WebForm1" % >
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
< HTML >
< HEAD >
< title >WebForm1< /title >
< meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1" >
< meta name="CODE_LANGUAGE" Content="C#" >
< meta name="vs_defaultClientScript" content="JavaScript" >
< meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5" >
< /HEAD >
< body MS_POSITIONING="GridLayout" >
< form id="Form1" method="post" runat="server" >
< asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 112px; POSITION: absolute; TOP: 16px"
runat="server" AutoGenerateColumns="False" BorderColor="#DEBA84" BorderStyle="None" CellSpacing="2"
BorderWidth="1px" BackColor="#DEBA84" CellPadding="3" >
< SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#738A9C" >< /SelectedItemStyle >
< ItemStyle ForeColor="#8C4510" BackColor="#FFF7E7" >< /ItemStyle >
< HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#A55129" >< /HeaderStyle >
< FooterStyle ForeColor="#8C4510" BackColor="#F7DFB5" >< /FooterStyle >
< Columns >
< asp:TemplateColumn HeaderText="Id" >
< ItemTemplate >
< asp:TextBox id="txtID" runat="server" Text='< %# DataBinder.Eval(Container.DataItem, "iEid ") % >' >
< /asp:TextBox >
< /ItemTemplate >
< /asp:TemplateColumn >
< asp:TemplateColumn HeaderText="Name" >
< ItemTemplate >
< asp:TextBox id="txtName" runat="server" Text='< %# DataBinder.Eval(Container.DataItem, "cEName") % >' >
< /asp:TextBox >
< /ItemTemplate >
< /asp:TemplateColumn >
< asp:TemplateColumn HeaderText="Salary" >
< ItemTemplate >
< asp:TextBox id="txtSal" runat="server" Text='< %# DataBinder.Eval(Container.DataItem, "dSalary") % >' >
< /asp:TextBox >
< /ItemTemplate >
< /asp:TemplateColumn >
< /Columns >
< PagerStyle HorizontalAlign="Center" ForeColor="#8C4510" Mode="NumericPages" >< /PagerStyle >
< /asp:DataGrid >
< asp:Button id="btAdd" style="Z-INDEX: 102; LEFT: 64px; POSITION: absolute; TOP: 24px" runat="server"
Text="Add" >< /asp:Button >
< /form >
< /body >
< /HTML >

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 TestDGrid
{
///
/// Summary description for WebForm1.
///

public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected DataTable table = new DataTable();
protected System.Web.UI.WebControls.Button btAdd;
SqlConnection con = new SqlConnection(@"Server=IISSERVER;DataBase=TestSenthil;uid=sa;pwd=sa;");
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if(!IsPostBack)
{ fill();
BindDG();
}
}

public void fill()
{
SqlDataAdapter da = new SqlDataAdapter("Select * from emp",con);
DataSet ds = new DataSet();
da.Fill(table);
}

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.
///

private void InitializeComponent()
{
this.btAdd.Click += new System.EventHandler(this.btAdd_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void btAdd_Click(object sender, System.EventArgs e)
{
DataGrid1.CurrentPageIndex = 0;
fill();
Nrow();
BindDG();
}
}
}

No comments: