473,394 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How to dynamically add a row to Datagrid?

I have a datagrid that reads several rows of data from the database and loads
them on the screen.
Now I want to dynamically add a new row and assign the cells in the new row,
values.

When I try to create a datagridItem, I use the following call:
int newIndex = datagrid1.Items.Count; // returns 15 the current number of
rows in the grid
datagrid1.Items[newIndex].DataItem = new DataGridItem(newIndex, ?,
ListItemType.Item);

I get an error on the first parameter. It says that my index should not be
zero and be less than the collection count. (while I know that newIndex is
15) I don't know what to put for the 2nd parameter since I am not reading the
data for this row from database(datasource). I am pretty sure the third
parameter is correct.

After and if I can get this to work, how do I bind the new data to this row?

ANy help will be greatly appreciated.

Thanks
Any help is greatly appreciated.
Nov 19 '05 #1
1 6114


"Merdaad" wrote:
I have a datagrid that reads several rows of data from the database and loads
them on the screen.
Now I want to dynamically add a new row and assign the cells in the new row,
values.

When I try to create a datagridItem, I use the following call:
int newIndex = datagrid1.Items.Count; // returns 15 the current number of
rows in the grid
datagrid1.Items[newIndex].DataItem = new DataGridItem(newIndex, ?,
ListItemType.Item);

I get an error on the first parameter. It says that my index should not be
zero and be less than the collection count. (while I know that newIndex is
15) I don't know what to put for the 2nd parameter since I am not reading the
data for this row from database(datasource). I am pretty sure the third
parameter is correct.
If you are reading the data for this row at the postback event from other
controls on the webform (e.g. TextBox, DropDownList) then you can create a
new row in the DataTable that you bound to the DataGrid then call again the
DataGrid.DataBind method. This would create the new DataGridItem for your
automatically.

DataRow myRow;
myRow = myDataTable.NewRow();
// put statements to assign the values to the dr.Items collection then add
it to the Table
myDataTable.Rows.Add(myRow);
//DataBind
MyDataGrid.DataSource = myDataTable;
MyDataGrid.DataBind();

---
Phillip Williams
http://www.societopia.net
http://www.webswapp.com

After and if I can get this to work, how do I bind the new data to this row?

ANy help will be greatly appreciated.

Thanks
Any help is greatly appreciated.

Nov 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: sameer mowade via .NET 247 | last post by:
Hello All, I have problem while dynamically removing row from the Datagrid which i have added dynamically as shown in the following code snippet. The problem is that while removing dynamically...
6
by: John Ruiz | last post by:
Greetings, I originally posted this to microsoft.public.dotnet.framework.aspnet.datagridcontrol two weeks ago, but no one was able to answer. I am unable to dynamically add columns to a...
4
by: bill | last post by:
Can I add multiple datagrid controls dynamically at runtime? I want to break up my dataset into separate datagrids, so I can insert a "<p style="page-break-before: always"> tag between them. ...
4
by: dyw55a | last post by:
Donna Mar 15, 10:11 am show options Newsgroups: microsoft.public.dotnet.framework.adonet From: "Donna" <dyw...@yahoo.com> - Find messages by this author Date: 15 Mar 2005 10:11:56 -0800...
4
by: Shiju Poyilil | last post by:
Hello EveryBody, I have a dropdownlist with some values as .net server controls & a button , on clicking a button i need to generate the choosen contorl dynamically. I am giving a small glimpse...
3
by: david | last post by:
I have posted my question before. It seems that I can not find the solution. The question: I have datasource, say, ds which is bounded to a datagrid, dg. Assume that ds have 5 columns,...
0
by: news.zen.co.uk | last post by:
Hi Guys, Using VB .NET 2003 and ASP1.1, I have a Datagrid embedded in an ASP page. In the processing of the ItemDataBound event I dynamically create a new Datagrid control within the Cell of the...
4
by: sydney.luu | last post by:
Hello, I would greatly appreciate if someone can show me how to dynamically build a Repeater with unknown number of columns at design time. I have looked various threads in this newsgroup,...
2
by: Pete Moss | last post by:
During a postback event, I am having trouble retrieving the state of a CheckBox Control that I am dynamically adding to a DataGrid Control using ASP.NET 1.1. I have no trouble adding the...
3
by: rn5a | last post by:
A SqlDataReader is populated with the records from a SQL Server 2005 DB table. The records retrieved depends upon 2 conditions (the conditions depend on what a user selects in an ASPX page). If...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.