How to get DataRow array into a DataView?

D

deko

I need to get the child rows of a DataTable using the foreign key, and then
I need to sort those rows. So I want to get the array of DataRows returned
by GetChildRows into a DataView. Is it possible to insert the DataRow array
into a DataView? Will I have to put each row in the array into a table
first?

dtPrj = database.Tables[tblPrj];
DataRow[] drArray;
DataRow dr = dtPrj.Rows.Find(definitionID);
drArray = dr.GetChildRows(fkPC);

How to get the drArray rows into a DataView?

Thanks in advance.
 
W

W.G. Ryan eMVP

The DataView is based on a DataTable so technically speaking, what you want
to do isn't possible. A DataView has to be based on a DataTable, not a
DataRow. However there's a straightforward work around to get you to the
same place. So when I say it isn't possible only 'technically' (you can't
create a view off of a datarow). What you can do is create a DataView based
on a condition. So what you can do is get the value of the parent row that
corresponds to the related rows you want, and set the rowfilter to it. This
will give you all the rows you want in the child table in a dataview of its
own. Let's say that the Parent Value is 10001 and the field name for the
PK/FK is named CustomerNumber.

You could do something like this...

DataView ChildRows = database.Tables[tblPrj].DefaultView;
ChildRows.RowFilter = "CustomerNumber = 10001';//or you can concatenate the
RowFilter string so that you don't hard code in 10001. You could use, in
your example, after the line where you called the Find method..

ChildRows.RowFilter = "CustomerNumber = " + dr["CustomerNumber"].ToString();

Now, you'll have a DataView filled only with records that have a
CustomerNumber corresponding to the Parent Row, in this case, the one with a
value of 10001.

Is this the end result you were looking for? If not let me konw and I'll see
what I can do
 
D

deko

The DataView is based on a DataTable so technically speaking, what you
want to do isn't possible. A DataView has to be based on a DataTable, not
a DataRow. However there's a straightforward work around to get you to
the same place. So when I say it isn't possible only 'technically' (you
can't create a view off of a datarow). What you can do is create a
DataView based on a condition. So what you can do is get the value of the
parent row that corresponds to the related rows you want, and set the
rowfilter to it. This will give you all the rows you want in the child
table in a dataview of its own. Let's say that the Parent Value is 10001
and the field name for the PK/FK is named CustomerNumber.

You could do something like this...

DataView ChildRows = database.Tables[tblPrj].DefaultView;
ChildRows.RowFilter = "CustomerNumber = 10001';//or you can concatenate
the RowFilter string so that you don't hard code in 10001. You could use,
in your example, after the line where you called the Find method..

ChildRows.RowFilter = "CustomerNumber = " +
dr["CustomerNumber"].ToString();

Now, you'll have a DataView filled only with records that have a
CustomerNumber corresponding to the Parent Row, in this case, the one with
a value of 10001.

Hi and thanks for the well-written reply.

From what you've explained so well, I think this should do the trick:

dtCfg = Database.Tables[tblCfg];
DataView dv = dtCfg.DefaultView;
dv.RowFilter = string.Concat(colCid, " = ", definitionID);

//colCid, dtCfg, and tblCfg are string consts

dtCfg is the other end of fkPC in my previous example. So I just use
dv.RowFilter on dtCfg rather than dr.GetChildRows on the dtPrj table.

But now I need to loop over these rows. Can I loop over the DataView, or do
I need to do something like this:

DataTable cSort = dv.ToTable();

Thanks for the help.
 
D

deko

You cannot get anything into a DataView.
You can select and sort datarows with a DataView and use that to show
To get another DataRowCollection you can use the Datatable.Select

http://msdn2.microsoft.com/en-us/library/det4aw50.aspx

I am using the DataView mainly for sorting.

dtPrj = database.Tables[tblPrj];
DataView dv = dtPrj.DefaultView;
string[] pNames = { colPN, colPid };
DataTable pSort = dv.ToTable(false, pNames);
int index = 0;
foreach (DataRow dr in pSort.Rows)
{
.... do stuff with those two columns...

Is there a way to loop over a DataView rather than sending it back to a
DataTable?
 
C

Cor Ligthert [MVP]

Deko,

I think that you confuses use by telling the method you want to use instead
of the result. I have the idea that as you tell us the result you want to
reach (and than not in abrevations as colPd however just that Pd what that
means). I think that it will be than easier to help you.?

Cor

deko said:
You cannot get anything into a DataView.

You can select and sort datarows with a DataView and use that to show
To get another DataRowCollection you can use the Datatable.Select

http://msdn2.microsoft.com/en-us/library/det4aw50.aspx

I am using the DataView mainly for sorting.

dtPrj = database.Tables[tblPrj];
DataView dv = dtPrj.DefaultView;
string[] pNames = { colPN, colPid };
DataTable pSort = dv.ToTable(false, pNames);
int index = 0;
foreach (DataRow dr in pSort.Rows)
{
... do stuff with those two columns...

Is there a way to loop over a DataView rather than sending it back to a
DataTable?
 
W

W.G. Ryan eMVP

Hi Deko:

You once you have the dataview, to iterate them you can use the ToTable()
method if you're using the 2.0 Framework. Then you'd just do a
foreach(dataRow dro in myTable.Rows) . Otherwise you can use these
approaches (these will work in any version of the framework
http://www.knowdotnet.com/articles/iteratingdataview.html)

Let me know how it goes ;-) or i fyou need any help
deko said:
The DataView is based on a DataTable so technically speaking, what you
want to do isn't possible. A DataView has to be based on a DataTable, not
a DataRow. However there's a straightforward work around to get you to
the same place. So when I say it isn't possible only 'technically' (you
can't create a view off of a datarow). What you can do is create a
DataView based on a condition. So what you can do is get the value of the
parent row that corresponds to the related rows you want, and set the
rowfilter to it. This will give you all the rows you want in the child
table in a dataview of its own. Let's say that the Parent Value is 10001
and the field name for the PK/FK is named CustomerNumber.

You could do something like this...

DataView ChildRows = database.Tables[tblPrj].DefaultView;
ChildRows.RowFilter = "CustomerNumber = 10001';//or you can concatenate
the RowFilter string so that you don't hard code in 10001. You could use,
in your example, after the line where you called the Find method..

ChildRows.RowFilter = "CustomerNumber = " +
dr["CustomerNumber"].ToString();

Now, you'll have a DataView filled only with records that have a
CustomerNumber corresponding to the Parent Row, in this case, the one
with a value of 10001.

Hi and thanks for the well-written reply.

From what you've explained so well, I think this should do the trick:

dtCfg = Database.Tables[tblCfg];
DataView dv = dtCfg.DefaultView;
dv.RowFilter = string.Concat(colCid, " = ", definitionID);

//colCid, dtCfg, and tblCfg are string consts

dtCfg is the other end of fkPC in my previous example. So I just use
dv.RowFilter on dtCfg rather than dr.GetChildRows on the dtPrj table.

But now I need to loop over these rows. Can I loop over the DataView, or
do I need to do something like this:

DataTable cSort = dv.ToTable();

Thanks for the help.
 
W

W.G. Ryan eMVP

Deko:

You can iterate a dataview using a few methods other than grabbing it's base
table:

IEnumerator iterator = dv.GetEnumerator();
DataRowView drv;
System.Int32 i= 0;
while(iterator.MoveNext())
{
drv = (DataRowView)iterator.Current;
i++;
}
System.Int32 x = 0;

foreach(DataRowView drv2 in dv)
{
x++;
}
deko said:
You cannot get anything into a DataView.

You can select and sort datarows with a DataView and use that to show
To get another DataRowCollection you can use the Datatable.Select

http://msdn2.microsoft.com/en-us/library/det4aw50.aspx

I am using the DataView mainly for sorting.

dtPrj = database.Tables[tblPrj];
DataView dv = dtPrj.DefaultView;
string[] pNames = { colPN, colPid };
DataTable pSort = dv.ToTable(false, pNames);
int index = 0;
foreach (DataRow dr in pSort.Rows)
{
... do stuff with those two columns...

Is there a way to loop over a DataView rather than sending it back to a
DataTable?
 
G

Guest

Hi deko,

You can loop over dataview by

foreach (DataRowView drv in dv)
{
int intData = (int)drv["FIELD_NAME"]; // or (int)drv[FIELD_INDEX];
// or other type of data
}

HTH

Elton Wang


deko said:
You cannot get anything into a DataView.

You can select and sort datarows with a DataView and use that to show
To get another DataRowCollection you can use the Datatable.Select

http://msdn2.microsoft.com/en-us/library/det4aw50.aspx

I am using the DataView mainly for sorting.

dtPrj = database.Tables[tblPrj];
DataView dv = dtPrj.DefaultView;
string[] pNames = { colPN, colPid };
DataTable pSort = dv.ToTable(false, pNames);
int index = 0;
foreach (DataRow dr in pSort.Rows)
{
.... do stuff with those two columns...

Is there a way to loop over a DataView rather than sending it back to a
DataTable?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top