Strongly Typed DATASETs...

D

Davide Piras

Hi, I've just started using ST Datasets and I've a question:

how to get a field name (column name) from a ST DataTable contained in my ST
DataSet ?

What I mean is: should I "cable" the ColumnName as string whene I assign
DisplayMember and ValueMember of ComboBox or I can use a property of my ST
Object??

In fact, I do not like to write

mycombo.DataSource = dsPeople.PeopleDataTable;
mycombo.DisplayMember = "Name";
mycombo.ValueMember = "ID";

because if I will change the struct of my ST Objects (dsPeople and/or
PeopleDataTable), my code will still compile so there is no advantage in
using ST Objects... if I should still write FieldNames as string...

Any idea?

Thank in advantage, happy development, Davide.
 
G

Guest

Hi Davide,

You can do this to keep things generic:

mycombo.DisplayMember = dsPeople.PeopleDataTable.Columns(dm).ColumnName
mycombo.ValueMember = dsPeople.PeopleDataTable.Columns(vm).ColumnName

where dm is the integer column index of the DisplayMember in your dataTable,
and vm is the integer column index of the ValueMember. That way you can
change names and not have to rewrite code. You still have to rewrite if you
change which column your displaymember and/or valuemember occupy in the
datatable.

JT
 
D

Davide Piras

Good suggestion, thanks...

Actually I was thinking about the opportunity to have a public property,
somewhere, that returns the column name as string...

I mean:

dsPeople.PeopleDataRow.Name returns a string with the Name of that person...

dsPeople.PeopleDataTable.NameColumnName, does not exists, but it could be
good to have it, so I use it to retrieve a string with "Name" (the column
name)...

Now, If I change my data struct... my code do not compile because Both:

dsPeople.PeopleDataRow.Name
dsPeople.PeopleDataTable.NameColumnName

do not exist anymore... in this way, no integer and no strings lost and
unuseful around...

P.S. anyway if nobody is going to give any better suggestion, I will use the
integer way like you said!

Davide.
 
K

Kris Desmadryl

To your ST Dataset you add two columns, one called DisplayMember and the
other ValueMember.
In the expression of the column you put the actual columnName.
The code is then always :
mycombo.DataSource = dsPeople.PeopleDataTable;
mycombo.DisplayMember = "DisplayMember ";
mycombo.ValueMember = "ValueMember";

In the expression of the displaymember you could even put something like
FirstName + ' ' + LastName

Kris.
 
D

Davide Piras

GOOD!
This is SMART!!

So I eventually will change only the ST Dataset/Datatable, if I will need to
change the Display/Value Member...

Thanks!
 

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