Solved

Copy rows from Datatable

Posted on 2013-01-28
3
293 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project

828 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