Advertisement

03.10.2006 at 04:20PM PST, ID: 21769398
[x]
Attachment Details

How to convert DataRow[] into a datatable...

Asked by kalliopi in C# Programming Language

Tags: convert, datatable, datarow, array

I'm starting to try to fully take advantage of the very powerful "DataSet" structure in .net.  I've learned a few things in the last few days, like how to get child rows based on a relationship.  The call:

DataTable.GetChildRows(...) returns an array of DataRow.  The question is, what's the easiest way to get these data rows back into a format that can be used as the datasource for a databound control (like a datagrid)?  My current solution (which feels like a hack) is:

DataTable dt = DataSet1.Tables[0];

DataRow[] rows = dt.GetChildRows(...);
DataTable dt2 = dt.Clone();
foreach (DataRow row in rows) dt2.Rows.Add(row);
DataGrid1.DataSource = dt2;

// What I really want to be able to do, is something more like this:


DataTable dt = DataSet1.Tables[0];

DataGrid1.DataSource = (DataTable) dt.GetChildRows(...);
// or
DataGrid1.DataSource = new DataTable(dt.GetChildRows(...));
// or
DataGrid1.DataSource = ???

Any guidence is greatly appreciated.  Take care.
Start Free Trial
[+][-]03.10.2006 at 04:47PM PST, ID: 16160492

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: C# Programming Language
Tags: convert, datatable, datarow, array
Sign Up Now!
Solution Provided By: carl_tawn
Participating Experts: 2
Solution Grade: A
 
 
[+][-]03.11.2006 at 12:28AM PST, ID: 16161936

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.11.2006 at 12:46AM PST, ID: 16161958

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.11.2006 at 01:38AM PST, ID: 16162055

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]03.11.2006 at 08:05AM PST, ID: 16163298

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.11.2006 at 09:58AM PST, ID: 16163734

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32