Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Datatable - Adding Rows.

Posted on 2014-10-08
5
Medium Priority
?
164 Views
Last Modified: 2014-11-18
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
Comment
Question by:andyw27
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 12

Expert Comment

by:Ramkisan Jagtap
ID: 40368180
Can you please provide the structure of your table?
0
 

Author Comment

by:andyw27
ID: 40368236
Hi,

Here is the data structure

            dtRaw.Columns.Add("parent_id", typeof(int));
            dtRaw.Columns.Add("component_id", typeof(int));
            dtRaw.Columns.Add("position", typeof(string));
            dtRaw.Columns.Add("level", typeof(int));
            dtRaw.Columns.Add("check", typeof(int));
            dtRaw.Columns.Add("qty", typeof(int));
0
 
LVL 2

Accepted Solution

by:
Vishal Patil earned 2000 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;
            }

Open in new window

0
 
LVL 2

Expert Comment

by:Vishal Patil
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++;
            }

Open in new window

0
 
LVL 40
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question