473,411 Members | 2,230 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,411 software developers and data experts.

How to Insert New Row Into Existing DataTable While Looping DataTable

Dear All,

Please help.
Same as subject !!!
Nov 16 '05 #1
4 34548
Kelvin wrote:
Dear All,

Please help.
Same as subject !!!


Inserting a new row is the same whether you are looping or not.

DataTable dt = new DataTable();
DataRow row = dt.NewRow();
row["columnname"] = "whatever";
dt.Rows.Add(row);
dt.AcceptChanges();

If you are looking for something more specific to your situation, then
you need to post some code.

Lowell
Nov 16 '05 #2
Hi,

No really, if you are iterating a collection you cannot change it, or you
may get an exception.

IIRC I had a similar problem but with removing instead of adding, solution:
Use an ArrayList to keep the rows that you will add
after the loop add all the rows from the ArrayList into the datatable.
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Lowell Heddings" <lo****@mindjunction.com> wrote in message
news:Oc**************@TK2MSFTNGP14.phx.gbl...
Kelvin wrote:
Dear All,

Please help.
Same as subject !!!


Inserting a new row is the same whether you are looping or not.

DataTable dt = new DataTable();
DataRow row = dt.NewRow();
row["columnname"] = "whatever";
dt.Rows.Add(row);
dt.AcceptChanges();

If you are looking for something more specific to your situation, then you
need to post some code.

Lowell

Nov 16 '05 #3
Hi,

Use an ArrayList while you are looping and later add them to the table

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Kelvin" <ke*******@gmail.com> wrote in message
news:db*************************@posting.google.co m...
Dear All,

Please help.
Same as subject !!!

Nov 16 '05 #4
Ignacio,

Wouldn't it be easier to do this?

// Get the number of rows.
// dt is the data table.
int rows = dt.Rows.Count;

// The data row.
DataRow row;

// Cycle
for (int index = 0; index < rows; ++index)
{
// Get the row.
row = dt.Rows[index];

// Add row if necessary.
}

This way, you avoid an extra loop, and you prevent processing of the
added rows.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:%2****************@TK2MSFTNGP12.phx.gbl...
Hi,

Use an ArrayList while you are looping and later add them to the table

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Kelvin" <ke*******@gmail.com> wrote in message
news:db*************************@posting.google.co m...
Dear All,

Please help.
Same as subject !!!


Nov 16 '05 #5

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

Similar topics

14
by: serge | last post by:
I have a scenario where two tables are in a One-to-Many relationship and I need to move the data from the Many table to the One table so that it becomes a One-to-One relationship. I need to...
1
by: Kelvin | last post by:
Hi All, While the existing datatable is looping, I can insert new row into datatable, but I can't insert it between datarows. All new rows will inserted into buttom row. Please advise!
1
by: Vinny Vinn | last post by:
I would like to insert a new column in DataTable at a specified position at runtime. Unfortunatly, the DataColumnCollection does not expose any method to do this (like AddAt),Any ideas would be...
4
by: Prince | last post by:
Hi All, Can anyone tell me how to Insert,Update and Delete a record into already existing DataTable? Looking forward for the reply... Thanx in advance... Regards,
2
by: John Smith | last post by:
i dont understand how to use `em all those @id ... what they have to do with a datagrid bound to a dataset filled by an adapter TIA
3
by: Ed | last post by:
Hi, I want to load data to a table in Sql Server from a dataset table in my vb.net app using a dataAdapter. I know how to do this as follows (my question is to see if I can reduce the amount...
13
by: =?Utf-8?B?UGV0ZXI=?= | last post by:
I want to create a new column in a datatable from two existing columns. I have no problem to create the new column using the datatable.columns.add method. The problem is the value of the new...
13
by: Terry Olsen | last post by:
I'm using OleDb to connect with an Access Database. I have anywhere from 10 to over 100 records that I need to either INSERT if the PK doesn't exist or UPDATE if the PK does exist, all in a single...
7
by: anu b | last post by:
Hi I need to use Clr trigger for insert command My code is as below I am using SQL server 2005 and VS 2008.... but after running this code i didnt get the result as i expexted it shows the...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.