DataKeyField with a composite key

  • Thread starter Thread starter rufus
  • Start date Start date
R

rufus

Hi,

I was wondering how a DataGrid can handle a DataSet with a composite key.
How would I update 1 row in a database having a composite key?

A possible solution would be to concatenate my composite key in my sql
statement and then use the split function to separate again when I update.

To me this seems too complicated for its own good. Is there a simpler
solution that I could implement?

Thanks in advance.

Chris
 
VB.2005 will add this.

Here is my work around. I just append an autoincrementing primarykey field
to the datatable prior to filling it from db. I use this new field in my
datagrid DataKeyField.

(Warning: this won't help push the changes back to the db using
dataadapter's Update method. I happen to be doing my updates manually.)

Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)

Dim dt As New DataTable

Dim myColumn As DataColumn = New DataColumn("MyID")
myColumn.ReadOnly = True
myColumn.DataType = System.Type.GetType("System.Int32")
With myColumn
.AutoIncrement = True
.AutoIncrementSeed = 1
.AutoIncrementStep = 1
End With
' Add the column to a new DataTable.

dt.Columns.Add(myColumn)

Dim myKey(0) As DataColumn
myKey(0) = dt.Columns("MyID")

dt.PrimaryKey = myKey

Try
da.Fill(dt)
...

HTH,
Greg
 
Back
Top