Multiple primary keys with a datagrid?

G

Guest

Hi,

Maybe I'm missing something with the DataKeyField attribute of a datagrid
but it seems that it's somewhat limiting since this only allows you to
specify one field as the key.

I have a table that has two keys and it seems you need this value to find
the corresponding record in the DataSet in order to update the data from the
Datagrid.

Thanks for any ideas on this. Dave.
 
K

-Karl

I wonder why on earth you would ever want 2 PRIMARY keys in a DB. I
mean, how do you determine which field is valid now that either one
could be PRIMARY.

Primary keys should be and unless I'm wrong here, 99.99% of the time,
only 1 of them in a table.
 
G

Guest

Look at the 'Order Details' table in Northwind or 'titleauthor' table in Pubs
to see what I'm talking about.

The example I had in mind was if you have to update a quantity for an Order
Detail that was displayed in the a DataGrid since the table has multiple
columns identified as a primary key.
 
F

Francois

Maybe Dave is using a wrong terminology and means that he has a table with a
composite Primary key consisting of 2 fields. That is totally valid on the
DB point of view and the combination of the 2 fields IS the primary key.

Also, Karl, primary keys are unique 100% of the time... if you think about
it, it would be scary if it was only 99.99 % of the time ;)

Hence the question of Dave is totally valid: how do you define a composite
primary key in the Datagrid DataKeyField property? Is it possible at all? If
not what is the best way?
I used to do stuff like having an invisible control in the row containing
the PK but I consider that as hacking and not a nice way to do it.
Maybe a smarter way is to have a serializable object that you store in the
viewstate of the DataGridItem (the row of the datagrid). But I never tried
it that way. Dave, if you try it like that, let me know if it works well.

Hope it helps,

Francois
 
G

Guest

Thanks for the feedback. You're right, I should have said "I have a table
that has two columns defined as the primary key".

Dave.
 
F

Francois

That table is exactly an example where the primary key is a composite PK.

You can see on my other post in this thread for possible suggestions.

cheers,

Francois.
 
K

-Karl

I'll have to see what you mean as I always understood DB's to be able
to have only 1 primary key to make sure there is absolutly no chance of
duplicates / data corruption.

what I meant by the 99.99% is that I was 99.99% sure that all db's
would have only 1 primary key.

I haven't touched upon composite PK so I do not know anything about
that terminology. Thanks for pointing it out. I'm just a noob :)
 
F

Francois

Ok, I am sorry for the 99.99 % thing, I misunderstood what u meant by that.

But an information that can be useful for you for later is that composite
primary key can be very useful for expressing a many to many relationships
between 2 tables.
Let's take a football (soccer if u are on the other side of the ocean)
example.
One league can have more than one team. But one team can belong to more than
one league. For example, the English Premier League (EPL) got obviously more
than one team. But one team can belong to more than one league as it could
belong for example to the EPL and the UEFA Champion's League.
But still you cannot permit that the same team / league combination appears
2 times in the DB as it would mean that one team appears 2 times in the same
league which obviously does not make sense. You then need to have a
composite Primary key to make that impossible to happen.

I hope that helps you understanding Composite PK. There are a lot of
litterature about it all over the internet if you want to see example and
all, it really worth it as you often need it in real world applications.

Cheers

Francois.

PS: By the way what does "noob" mean?
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top