• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 9879
  • Last Modified:

Datatable.Select() into a new Datatable??

Ok,

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 datatable.select("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?
Kris
0
kdunnett
Asked:
kdunnett
1 Solution
 
naveenkohliCommented:
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);
}
0
 
SystemExpertCommented:
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;
      }
    }
0
 
SystemExpertCommented:
or this is very nice code for you

http://www.akadia.com/services/dotnet_filter_sort.html

Thanks
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
gregoryyoungCommented:
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.

Cheers,

Greg
0
 
kdunnettAuthor Commented:
naveenkohli,

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?

Kris
0
 
ASD_TRANECommented:
kdunnett

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

0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now