Solved

Copy rows from Datatable

Posted on 2013-01-28
3
290 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

744 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

10 Experts available now in Live!

Get 1:1 Help Now