|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
DataTable.Select() - is it possible to pass rows back to datatablemethod, 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(); 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: Show quote > 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(); 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 Show quote "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(); > Andre Ranieri wrote:
> Matt, Perhaps the rows in your 'Categories' table aren't stored physically in > > 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? 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? Is "Class1" an actual object, or the name of a class? The error you are > 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 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 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 Show quote "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 > 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; |
|||||||||||||||||||||||