Hi,

I have displayed firstname, lastname and city fields in the datagrid. In these, first two are bound columns and the city is template column which has dropdown list control.

When i click "Add new row" button, i need to add one dynamic row which has two textbox and one dropdown fields within the datagrid. After entering the value, it will br displayed in the datagrid.

Plz help me how to do this..

Thanks in advance.

Satees

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

CodeBehing

I Used SqlServer 7, use your Servername and Database name

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>
	/// Summary description for WebForm1.
	/// </summary>
	
	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=Servername;DataBase=database name;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);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		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();
			
		}
	}
}
This article has been dead for over six months. Start a new discussion instead.