User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the ASP.NET section within the Web Development category of DaniWeb, a massive community of 402,003 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,370 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our ASP.NET advertiser: Lunarpages ASP Web Hosting
Views: 13553 | Replies: 4
Reply
Join Date: Nov 2006
Posts: 15
Reputation: satees is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
satees satees is offline Offline
Newbie Poster

How to add dynamic row in datagrid?

  #1  
Mar 21st, 2007
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
Thanks,
Satees
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jan 2007
Posts: 4
Reputation: windows_mss is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
windows_mss's Avatar
windows_mss windows_mss is offline Offline
Newbie Poster

Re: How to add dynamic row in datagrid?

  #2  
Mar 22nd, 2007
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();
			
		}
	}
}
NJoy Programming
Success Be Yours
Reply With Quote  
Join Date: Nov 2006
Posts: 15
Reputation: satees is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
satees satees is offline Offline
Newbie Poster

Re: How to add dynamic row in datagrid?

  #3  
Apr 5th, 2007
Hi windows_mss,

Yes, i got the solution. Thank u very much.
Thanks,
Satees
Reply With Quote  
Join Date: Nov 2007
Posts: 1
Reputation: msk_madhu2000 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
msk_madhu2000 msk_madhu2000 is offline Offline
Newbie Poster

Re: How to add dynamic row in datagrid?

  #4  
Nov 29th, 2007
hi satees

Can u mail me the source code/project using dropdown list control for adding dynamic rows
at msk.madhu2000@gmail.com

Thanks
Madhsudan
Reply With Quote  
Join Date: Dec 2003
Location: Nashville, TN
Posts: 2,333
Reputation: alc6379 has a spectacular aura about alc6379 has a spectacular aura about alc6379 has a spectacular aura about 
Rep Power: 11
Solved Threads: 101
Colleague
alc6379's Avatar
alc6379 alc6379 is offline Offline
Cookie... That's it

Re: How to add dynamic row in datagrid?

  #5  
Dec 7th, 2007
He just pasted it right here! Can you not simply copy it and paste from this forum?
Alex Cavnar, aka alc6379
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb ASP.NET Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the ASP.NET Forum

All times are GMT -4. The time now is 9:20 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC