Solved

Copy rows from Datatable

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

Stressed Out?

Watch some penguins on the livecam!

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

717 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