Solved

Copy rows from Datatable

Posted on 2013-01-28
3
291 Views
Last Modified: 2013-02-22
Hi,

If I have datatable that looks like this:

Col 1      Col 3      Col 4      Col 5
1001      906      581      403
1001      538      822      330
1001      354      837      982
1001      649      861      955
1001      499      673      286
2005      803      899      594
2005      994      385      463
2005      279      272      141
3006      275      945      396
3006      448      392      180
3006      286      842      678
3006      802      718      880

(The real world example has thousands of records.)

I need to create a function that accepts an int (in this case Col 1) and returns a new datatable contains just the rows the match col 1.  For example if I passed 2005 I would expect the new datatable to look like this:

2005      803      899      594
2005      994      385      463
2005      279      272      141


Looking for the most efficient means of doing this?
0
Comment
Question by:andyw27
3 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 38827012
use Select to filter datatble based on field and value, then create new datatble with result datarows:

var datarows = dtData.Select("Col1=2005");

 var dtNew = dtData.Clone();

foreach (DataRow row in datarows) {
   dtNew.ImportRow(row);
}

Open in new window

0
 
LVL 1

Expert Comment

by:DoutorApedeuta
ID: 38827117
I think you could use something like this:

private DataTable GetDTByFilter(DataTable dt, int col, int match)
        {
            string expression;

            expression = "Col" + col.ToString() + "=" + match.ToString();
            DataRow[] foundRows;

            foundRows = dt.Select(expression);

            return (foundRows.CopyToDataTable());
        }
0
 
LVL 4

Expert Comment

by:Srinivasulu Muppala
ID: 38832160
Try this i hope it will help you.

private void button1_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("Col1"));
            dt.Columns.Add(new DataColumn("Col2"));
            dt.Columns.Add(new DataColumn("Col3"));

            dt.Rows.Add(new object[] { 2005, 803, 899 });


            for (int i = 0; i < 4; i++)
            {
                //loop here to calculate or get values.
                AddToDataTable(2006, ref  dt);
            }

        }
       
        private void AddToDataTable(int colValue, ref DataTable dt)
        {
            //do calculations here for 830,900 values
            string colValues = string.Format("{0},{1},{2}", colValue, 830, 900);
            dt.Rows.Add(colValues.Split(new char[] { ',' }));
        }
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

911 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

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now