views:

677

answers:

3

I know of existence of DataTable.Select(). It works great if you only need to find rows.

What if you need to find and modify existing rows? I can't think of any other approach than implementing my own search function.

Any input?

I am using Compact Framework 2.0, c#

A: 

Why can't you modify the rows retrieve through DataTable.Select()?

Jay Riggs
That's true, but you can update your DataTable through the DataRows returned by Select.
Jay Riggs
apparently I can - reusing Jonathan's code after Find()
gnomixa
Yes, I didn't know that. That's sort of what I was asking. Will try that now. i guess I was missing dt.LoadDataRow(dr.ItemArray, True)
gnomixa
+2  A: 

I would recommend not using the DataTable class at all and use POCO (plain old C# objects) and use List<obj> and leverage .Find() and .FindAll().

Take a look at http://www.gavaghan.org/blog/2007/07/17/use-inotifypropertychanged-with-bindinglist/ which explains the INotifyPropertyChanged, I believe most of the other interfaces are implemented in a similar way.

Chris Marisic
Do you mean "plain old CLR objects"?
Austin Salonen
/shrug, pretty interchangeable in this situation.
Chris Marisic
This recommendation is more of a deep scope than I have accounted deadline for. If someone asks a question, they are asking a question not asking for recommendation how to avoid this question. See the difference? Please stick to the question asked.
gnomixa
@gnomixa So my guess is you were the one that DV'd me and then above you said your considering changing it to a bindinglist, a bindinglist is an even more robust version of IList and to really have it wortwhile to use a bindinglist you have a large number of interfaces to implement to get anything out of it being more than just a bloated List class. Changing your application to use a bindinglist will be substantially more work than list especially if you aren't familiar with the patterns bindinglist uses.
Chris Marisic
thanks, i will take it to consideration. although you don't really provide much detail.
gnomixa
do you have any links regarding "patterns bindinglist uses"?
gnomixa
i added a link for you
Chris Marisic
+3  A: 

One strongly-typed method:

Dim dt As DataTable 'Get your DataTable populated with data
Dim dr As DataRow = dt.Rows.Find("pk1") 'Finds the row with this Primary Key
dr.BeginEdit() 'Transactional Update
dr.Item("columnName") = "someValue" 'Update this named column to someValue
dr.EndEdit() 'End Transaction
dt.LoadDataRow(dr.ItemArray, True) 'Update the datatable
Jonathan
Nothing against the poster, but does seeing this code make anyone else sick to their stomach? It sure does to me.
Chris Marisic
Seems tad harsh.
Jay Riggs
I didn't say this was the most efficient way. But in the compact framework, maintaining a List<T> object is more memory intensive than using built-in methods for a transactional update of a data table.When working on mobile environments your code must change a bit to accomodate. It's much worse in the BB environment where most everything needs to be stored in vectors.
Jonathan
Is there actually that much of a difference between List<person> and DataTable with person rows in it if they contain the exact same number of objects and property types on the person object?
Chris Marisic
In this case, that's not the point. But yes, a List<T> will be more memory intensive than a data table because the values are all objects themselves.The point here is that given a data table, you are creating another copy of the data in a List -- then filtering the list to select the object array -- then casting to a datarow -- then you still have to use .LoadDataRow to apply the update to the data table given.All in all, you are doing what I did, but instead of using the DataTable class, you are using the List class and spiking memory consumption however briefly.
Jonathan
Let me clarify the first sentence. All the members of a List are Reference types (Object or other type specified). The members of a datatable will be value types unless evaluated as strings.
Jonathan
I meant truly not use a DataTable at all not doing magic manipulations with DataRows and then somehow mashing it back into the table.
Chris Marisic
Chris, you are not making much sense. If you have a DataTable, then you have a DataTable. How are you going to "mash the rows back"? I am not quite sure where you are going with this:)
gnomixa
Jonathan, thanks. It so happens that I am toying with the idea of converting the DataTable to BindingList. So, (unrelated to this question), what you are saying is that the BindingList is more memory intensive? What are other cons/pros to use BindingList instead of DataTable specifically in Compact Framework.My app doesn't really remotely connect to anything while the grid is being manipulated, so speed is generally not a huge factor. It is installed on thin clients, rather traditional mobile devices.
gnomixa
What he was saying is your database function returns a DataTable you already allocated N memory. To then create a list object whether it's List or BindingList etc you will need N more memory and perhaps a little more fluff in the end, so you're using 2N memory. My point was to not have a DataTable at all and change your database calls to most likely use a DataReader where you pull one record through at a time and create a Person object or whatever it is and stuff that inside a List<Person>. You will need to follow that same model to use BindingList in a similar fashion.
Chris Marisic
What I was refering to on the DataTable and magic manipulations is previously I've worked with a legacy application built on DataTables and used lambda expressions combined with DataTable.Select() to return List<StronglyTypedDataRow> which is alot easier to work with for UI displaying however trying to save changes made to those DataRows is infuriating.The DataTable/DataSet classes are just horrible entities to work with, IMO they should be depreciated in the framework.
Chris Marisic
If you are using the Compact Framework and plan on returning a recordset from your datastore as a List<Type>, then the memory intensivity is moot in comparison to a datatable. My point was well explained by Chris above. You asked about DataTables, I explained how to perform the function requested.In general, I prefer Lists due to my preference towards generics, but I will use DataTables when trying to develop against a deadline.
Jonathan