Datatable.Select() into a new Datatable??

Posted on 2006-04-27
Last Modified: 2012-06-21

Its late in the day and i'm probably missing something here, so here's the question.

I want to put the result of a"condition", ""), into a another datatable, not into a datarow[].  Any ideas?

This is what I have:
string ID = "whatever";
DataRow[] foundRows = dt.Select("ID = "+ ID, "");

But what I want is:
string ID = "whatever";
DataTable dtNew = dt.Select("ID = "+ ID, "");

Any ideas?
Question by:kdunnett
    LVL 23

    Accepted Solution

    Try this..

    string ID = "whatever";
    DataTable dtNew = dt.Clone();

    DataRow [] drows = dt.Select("ID = "+ ID, "");

    for(int i = 0 ; i < drows.Length; i++)
        dtNew.LoadDataRow(drows[i], true);
    LVL 10

    Expert Comment

    private void BtnFilterAndSort_Click(object sender, System.EventArgs e)
          string  strText;
          string  strExpr;
          string  strSort;
          DataRow[] foundRows;
          DataTable myTable;
          myTable = ds.Tables["Orders"];
          // Setup Filter and Sort Criteria
          strExpr = "OrderDate >= '01.03.1998' AND OrderDate <= '31.03.1998'";
          strSort = "OrderDate DESC";
          // Use the Select method to find all rows matching the filter.
          foundRows = myTable.Select(strExpr, strSort);
          // Apply all Columns to the TextBox, this
          // must be done Row-By-Row.
          strText = null;
          for (int i = 0 ; i <= foundRows.GetUpperBound(0); i++)
            for (int j = 0; j <= foundRows[i].ItemArray.GetUpperBound(0); j++)
              strText = strText + foundRows[i][j].ToString() + "\t";
            strText = strText + "\r\n";
            textBox.Text = strText;
    LVL 10

    Expert Comment

    or this is very nice code for you

    LVL 37

    Expert Comment

    A good question is why you need the seperate data table? Could a different view of the same table work you? If so you can just use a DataView object then alter its RowFilter.



    Author Comment


    I'm getting an error on the line:
    dtNew.LoadDataRow(drows[i], true);

    And the error is:
    ...: Argument '1': cannot convert from 'System.Data.DataRow' to 'object[]'

    Seems like i would have to pass the object of drows without an index to it.  Does this seem right?


    Expert Comment


    You need to pass in drows[i].ItemArray instead of drows[i]


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Article by: Ivo
    Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
    Article by: Ivo
    C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    759 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

    14 Experts available now in Live!

    Get 1:1 Help Now