PC Review


Reply
Thread Tools Rate Thread

DataTable.Select() - is it possible to pass rows back to datatable

 
 
=?Utf-8?B?QW5kcmUgUmFuaWVyaQ==?=
Guest
Posts: n/a
 
      9th Nov 2005
After querying a data table into a DataRow array using DataTable.Select()
method, is there any way of updating the table with changes made to the
DataRow array?

In this example, rows in the original data table do not change even when I
change column values in the datarow array itself.

Also, the following code creates an untyped data row array. Is it possible
to create a datarow array that's strongly typed based on the strongly typed
datatable it's selected from?

Thanks in advance,

Andre Ranieri


DataRow[] r = Class1.myDS.Categories.Select("", "CatOrder");
for (int i =0; i < r.Length; i++)
{
lvCategories.Items.Add(r[i]["Category"].ToString().Trim());
r[i]["CatOrder"] = iCounter;
iCounter++;
}
Class1.myDS.AcceptChanges();
 
Reply With Quote
 
 
 
 
mabster
Guest
Posts: n/a
 
      9th Nov 2005
Andre,

Unless I'm missing something, you code should work fine. I update the
DataRow array returned from a DataTable.Select() call all the time, and
the changes flow through to the original DataTable/DataSet.

The rows returned from Select aren't copies - they're the actual rows in
the original table.

As for strongly-typed DataSets and DataRow arrays: Believe it or not,
you can just cast the result from Select, like this:

MyDataSet.MyTableRow[] rows;

rows = (MyDataSet.MyTableRow[])ds.MyTable.Select(...);

I was pleasantly surprised when I discovered that!

Cheers,
Matt

Andre Ranieri wrote:
> After querying a data table into a DataRow array using DataTable.Select()
> method, is there any way of updating the table with changes made to the
> DataRow array?
>
> In this example, rows in the original data table do not change even when I
> change column values in the datarow array itself.
>
> Also, the following code creates an untyped data row array. Is it possible
> to create a datarow array that's strongly typed based on the strongly typed
> datatable it's selected from?
>
> Thanks in advance,
>
> Andre Ranieri
>
>
> DataRow[] r = Class1.myDS.Categories.Select("", "CatOrder");
> for (int i =0; i < r.Length; i++)
> {
> lvCategories.Items.Add(r[i]["Category"].ToString().Trim());
> r[i]["CatOrder"] = iCounter;
> iCounter++;
> }
> Class1.myDS.AcceptChanges();

 
Reply With Quote
 
=?Utf-8?B?QW5kcmUgUmFuaWVyaQ==?=
Guest
Posts: n/a
 
      9th Nov 2005

Matt,

I must be doing something wrong then. When I step through the code block
the datarow array r[i]["CatOrder"] show the new value but the dataset
Class1.myDs.Categories[0]["CatOrder"] remains unchanged. Any ideas?


As far as the strongly typed datarow is concerned, is this the right syntax?
I'm getting a compile-time error when I try your suggestion. Intellisense
doesn't pick up Class1.myDS.CategoriesRows (my datatable name is Categories)
and the error says it denotes a field where a class was expected.

Class1.myDS.CategoriesRows[] r = Class1.myDS.Categories.Select("",
"CatOrder");

'SenskePreNote.Class1.myDS' denotes a 'field' where a 'class' was expected

Thanks,

Andre


"mabster" wrote:

> Andre,
>
> Unless I'm missing something, you code should work fine. I update the
> DataRow array returned from a DataTable.Select() call all the time, and
> the changes flow through to the original DataTable/DataSet.
>
> The rows returned from Select aren't copies - they're the actual rows in
> the original table.
>
> As for strongly-typed DataSets and DataRow arrays: Believe it or not,
> you can just cast the result from Select, like this:
>
> MyDataSet.MyTableRow[] rows;
>
> rows = (MyDataSet.MyTableRow[])ds.MyTable.Select(...);
>
> I was pleasantly surprised when I discovered that!
>
> Cheers,
> Matt
>
> Andre Ranieri wrote:
> > After querying a data table into a DataRow array using DataTable.Select()
> > method, is there any way of updating the table with changes made to the
> > DataRow array?
> >
> > In this example, rows in the original data table do not change even when I
> > change column values in the datarow array itself.
> >
> > Also, the following code creates an untyped data row array. Is it possible
> > to create a datarow array that's strongly typed based on the strongly typed
> > datatable it's selected from?
> >
> > Thanks in advance,
> >
> > Andre Ranieri
> >
> >
> > DataRow[] r = Class1.myDS.Categories.Select("", "CatOrder");
> > for (int i =0; i < r.Length; i++)
> > {
> > lvCategories.Items.Add(r[i]["Category"].ToString().Trim());
> > r[i]["CatOrder"] = iCounter;
> > iCounter++;
> > }
> > Class1.myDS.AcceptChanges();

>

 
Reply With Quote
 
mabster
Guest
Posts: n/a
 
      9th Nov 2005
Andre Ranieri wrote:

> Matt,
>
> I must be doing something wrong then. When I step through the code block
> the datarow array r[i]["CatOrder"] show the new value but the dataset
> Class1.myDs.Categories[0]["CatOrder"] remains unchanged. Any ideas?


Perhaps the rows in your 'Categories' table aren't stored physically in
CatOrder order?

What I mean is: When you call Select to retrieve your "r" array of
DataRows, you're asking for the array in CatOrder order.

So when you edit r[0]["CatOrder"] you may not actually be changing
Class1.myDs.Categories[0] ... you might be changing
Class1.myDs.Categories[15] or something.

Do you see what I mean?

> As far as the strongly typed datarow is concerned, is this the right syntax?
> I'm getting a compile-time error when I try your suggestion. Intellisense
> doesn't pick up Class1.myDS.CategoriesRows (my datatable name is Categories)
> and the error says it denotes a field where a class was expected.
>
> Class1.myDS.CategoriesRows[] r = Class1.myDS.Categories.Select("",
> "CatOrder");
>
> 'SenskePreNote.Class1.myDS' denotes a 'field' where a 'class' was expected


Is "Class1" an actual object, or the name of a class? The error you are
receiving tells me that something's wrong with the "Class1" identifier,
not the casting of one type to the next.

At any rate, the code should look more like this:

Class1.myDS.CategoriesRows[] r =
(Class1.myDS.CategoriesRows[])Class1.myDS.Categories.Select(
"", "CatOrder");

(I've chucked some newlines in there so that the word wrapping doesn't
make it too unreadable.)

What I *think* is happening with your error is that "Class1" is actually
a class, and you have to make an instance of it. Something along these
lines:

Class1 cl = new Class1();

MyDataSet.CategoriesRows[] r = (MyDataSet.CategoriesRows[])
cl.myDs.Categories.Select("", "CatOrder");

Does that look more correct?

Cheers,
Matt
 
Reply With Quote
 
=?Utf-8?B?QW5kcmUgUmFuaWVyaQ==?=
Guest
Posts: n/a
 
      9th Nov 2005
Matt,

It's correct, the categories are not necessarily stored in CatOrder order in
the datatable. The categories are displayed in a ListView, there are up and
down arrows next to the list view where the user can change the order of
precedence for the categories. Looking at the code with fresh eyes, I think
you've identified the problem: DataRow[0] is not the same as
Class1.myDS.Categories[0].

The myDS DataSet in my class is static; I'll play around with the code some
more this morning and see if I can't get this figured out.

Thanks again for your help.

Andre



"mabster" wrote:

> Andre Ranieri wrote:
>
> > Matt,
> >
> > I must be doing something wrong then. When I step through the code block
> > the datarow array r[i]["CatOrder"] show the new value but the dataset
> > Class1.myDs.Categories[0]["CatOrder"] remains unchanged. Any ideas?

>
> Perhaps the rows in your 'Categories' table aren't stored physically in
> CatOrder order?
>
> What I mean is: When you call Select to retrieve your "r" array of
> DataRows, you're asking for the array in CatOrder order.
>
> So when you edit r[0]["CatOrder"] you may not actually be changing
> Class1.myDs.Categories[0] ... you might be changing
> Class1.myDs.Categories[15] or something.
>
> Do you see what I mean?
>
> > As far as the strongly typed datarow is concerned, is this the right syntax?
> > I'm getting a compile-time error when I try your suggestion. Intellisense
> > doesn't pick up Class1.myDS.CategoriesRows (my datatable name is Categories)
> > and the error says it denotes a field where a class was expected.
> >
> > Class1.myDS.CategoriesRows[] r = Class1.myDS.Categories.Select("",
> > "CatOrder");
> >
> > 'SenskePreNote.Class1.myDS' denotes a 'field' where a 'class' was expected

>
> Is "Class1" an actual object, or the name of a class? The error you are
> receiving tells me that something's wrong with the "Class1" identifier,
> not the casting of one type to the next.
>
> At any rate, the code should look more like this:
>
> Class1.myDS.CategoriesRows[] r =
> (Class1.myDS.CategoriesRows[])Class1.myDS.Categories.Select(
> "", "CatOrder");
>
> (I've chucked some newlines in there so that the word wrapping doesn't
> make it too unreadable.)
>
> What I *think* is happening with your error is that "Class1" is actually
> a class, and you have to make an instance of it. Something along these
> lines:
>
> Class1 cl = new Class1();
>
> MyDataSet.CategoriesRows[] r = (MyDataSet.CategoriesRows[])
> cl.myDs.Categories.Select("", "CatOrder");
>
> Does that look more correct?
>
> Cheers,
> Matt
>

 
Reply With Quote
 
=?Utf-8?B?QW5kcmUgUmFuaWVyaQ==?=
Guest
Posts: n/a
 
      9th Nov 2005
Matt,

I wanted to let you know I figured out both issues, thanks for your help.

I confirmed that when you use the DataRow.Select() method you are really
passing the data from the datatable to the datarow array by reference; any
changes to the row array are automatically propagated back to the original
datatable; there is no need to write additional code to modify the datatable
itself.

Also, to create a strongly typed datarow array from a dataset, you need to
cast it from the base dataset object itself (myData, in my case), instead of
the instance (Class1.myDS).

I'm posting my working code here for future reference.

Thanks again,

Andre Ranieri

myData.CategoriesRow[] r =
(myData.CategoriesRow[])(Class1.myDS.Categories.Select("", "CatOrder"));

r[iIndex].CatOrder = r[iIndex].CatOrder - 1;
r[iIndex -1].CatOrder = r[iIndex -1 ].CatOrder + 1;


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DataTable.Select vs DataTable.rows.Find vs foreach Dave Microsoft C# .NET 1 17th May 2007 09:06 PM
encrypt a datatable pass it to a file and then later open it and have the datatable again, HOW? Mr. Richardson Microsoft C# .NET 2 7th Jun 2006 02:16 AM
How to assign the changed datatable in dataview back to a datatable? TaeHo Yoo Microsoft C# .NET 1 8th Jul 2005 04:41 AM
Retrieving datarows using DataTable.Select method and adding it to new DataTable C.Anand via DotNetMonster.com Microsoft ADO .NET 4 4th Apr 2005 05:21 PM
How can I use real SQL on a DataTable? i.e. not array of rows using a filter... as in DataTable.Select Dan V. Microsoft C# .NET 3 1st Jul 2004 03:06 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:20 PM.