Solved

Posted on 2014-10-08
149 Views
Hi,
I have a datatable with 5 rows
1
2
3
4
5

Can some one suugest some logic that would insert rows for each 5 so the end result would be
1
New Row
New Row
2
New Row
New Row
3
New Row
New Row
4
New Row
New Row
5
New Row
New Row

Thanks.
0
Question by:andyw27

LVL 12

Expert Comment

ID: 40368180
0

Author Comment

ID: 40368236
Hi,

Here is the data structure

0

LVL 2

Accepted Solution

Vishal Patil earned 500 total points
ID: 40368253
Hello andyw27,

You can use following code sample:

``````            int l = 1;
for (int i = 1; i < 6; i++)
{
DataRow dtr1 = dtRaw.NewRow();
dtr1["ID"] = "New Row";
dtr1["parent_id"] = 101; // just to recognise newly added data
dtr1["component_id"] = 102;
dtr1["position"] = "New Position";
dtr1["level"] = 5;
dtr1["check"] = 3;
dtr1["qty"] = 15;
dtRaw.Rows.InsertAt(dtr1, l);
l++;
DataRow dtr2 = dtRaw.NewRow();
dtr2["ID"] = "New Row";
dtr2["parent_id"] = 102; // just to recognise newly added data
dtr2["component_id"] = 302;
dtr2["position"] = "New Position";
dtr2["level"] = 5;
dtr2["check"] = 2;
dtr2["qty"] = 25;
dtRaw.Rows.InsertAt(dtr2, l);
l += 2;
}
``````
0

LVL 2

Expert Comment

ID: 40368256
Hello andyw27,

You can also use following code sample:

``````int k = 1;
for (int i = 1; i < 6; i++)
{

for (int j = 1; j <= 2; j++)
{
DataRow dtr1 = dtRaw.NewRow();
dtr1["ID"] = "New Row";
dtr1["parent_id"] = j + (i * 100); // just to recognise newly added data
dtr1["component_id"] = j + (i * 200);
dtr1["position"] = "New Position";
dtr1["level"] = i;
dtr1["check"] = i;
dtr1["qty"] = i;
dtRaw.Rows.InsertAt(dtr1, k);
k++;
}
k++;
}
``````
0

LVL 40

Expert Comment

ID: 40368952
I am not in a situation where I can test it, but the following should normally work, and this, no matter how many rows you have at the beginning:

Dim d As DataTable
Dim r As DataRow

For x As Integer = 0 To d.Rows.Count-1
d.Rows.InsertAt(d.NewRow, x * 3 + 1)
d.Rows.InsertAt(d.NewRow, x * 3 + 2)
Next
0

## Featured Post

### Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.