About using DataRow...

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

When I initialize my program I need to load from several sources of data
from files which are in a table form- in each set of data they share fields
(like say name address phone). I used to load them into a Hashtable keyed by
ID where the value is a class that has all the fields required (name,
address, etc...)

Now after discovering the power of DataTables and DataGrids I have decided
to load my data into that format instead, but much of my program still
depends on those concrete classes I had which had those fields and methods
specific to the data I load. With DataTables, that data had to be loaded into
this abstract DataRow which has no methods or fields specific to the data. So
now I'm wondering should I still keep my Hashtable of classes (Which begs the
question: when loading a DataRow does it actually copy my data from my
Hashtable or does it reference it?) or is there a way to customize DataRow so
I can have those specific fields and methods in the DataTable so I can get
rid of this HAshtable?
 
MrNobody,

I would say create a typed data set. You can use the designer to create
the data set structure in the editor, and then it will create a strongly
typed data set (with properties that you can access instead of using field
lookups). It should help with this sort of transition, and help reduce the
amount of code that you need to change.

Hope this helps.
 
Thanks for your advice!

I went into Design Mode and found the DataSet control in the toolbar- is
this whaat you meant? I tried creating one but when I want to do a typed
DataSet it needs a reference to one already in my project- of course this
dropdown is empty- what are the requirements so I can find my custom DataSet
in there? I created my DataTable programmatically, not via the Editor.

Or perhaps is there tutorial link you might know of off-hand to get me
started on this?

I am still not comfortable using the Data classes, as there's still a lot I
do not know, and any tutorials I find are based on untyped data loaded from a
database
 
You have to right click on your project, add new, and then select
dataset - then you can refrence the dataset with the dataset that you
actually place in your design view.
 
Benny Raymond said:
You have to right click on your project, add new, and then select
dataset - then you can refrence the dataset with the dataset that you
actually place in your design view.

Ok, so I do that and I create this empty DataSet, which does appear in that
dropdown now. But now how do I make this DataSet a strongly typed equivalent
to my old data classes?

Let's assume my data is name and address. My old class had those two fields
plus some methods to do some specific manipulation. How do I get this DataSet
to similate that?

On that DataSet editor, I only see XML related stuff on the toolbox, I don't
see how I can get it to describe my data or add properties so it is specific
to my data.

Thanks again for the help, boh of you!
 
Ok, I was playing around with it and it looks look I do have to use those XML
elements to describe my data. I added 3 SimpleTypes- 2 strings and 1 int.
After you create a type the rows beneath it are for adding data directly
right?

So now I'm not sure how to load my data using this new DataSet. I used to
create a DataTable which required creating specific columns. Then I'd start
reading from my file and for each record I'd have to call CreateRow and
AddRow on the DataTable.

How would I do this now with a strongly typed DataSet ? Aren't DataSets one
level above a table?
 
Hi,

Why don't you use casting instead of strong types?
you can:

DataRow dr = table.Rows[0];
string name = (dr["name"] as string).ToString(); // example method, you can
use any other method of any other cast
int number = (dr["number"] as int).CompareTo(0); // example method, you can
use any other method of any other cast

or you can:

string name = (string)dr["name"];
int number = (int)dr["number"];

Won't it work for you?

Tomer.
 
Back
Top