How can I programmaticaly add a column to a key.

  • Thread starter Thread starter Mr. X.
  • Start date Start date
M

Mr. X.

Hello.
I need to build a primary key (DataTable.primaryKey) programmatically,
but it has more then one column.
I didn't find any method for doing that.

How can I do that ?

Thanks :)
 
Am 08.07.2010 17:26, schrieb Mr. X.:
Hello.
I need to build a primary key (DataTable.primaryKey) programmatically,
but it has more then one column.
I didn't find any method for doing that.

How can I do that ?

The type of DataTable.primaryKey is an array. An array can contain
more items.
 
Yes, I know.

but I did something like this :
myTable.PrimaryKey = New DataColumn() {}
myTable.PrimaryKey.SetValue(New DataColumn("aKey"), 0)

.... and I get out of bounds on the second line.
What is the correct syntax?

Thanks :)
 
Am 08.07.2010 19:53, schrieb Mr. X.:
Yes, I know.

but I did something like this :
myTable.PrimaryKey = New DataColumn() {}
myTable.PrimaryKey.SetValue(New DataColumn("aKey"), 0)

... and I get out of bounds on the second line.
What is the correct syntax?

myTable.PrimaryKey = New DataColumn() {firstcolumn, secondcolumn}

Otherwise you assign an empty erray and SetValue fails because
there is no space in the array.
 
I don't know how much columns will be on first time, and I want dynamically
to change it (I don't know if there are two, or ten,
so {firstColumn, secondColumn} may not be the best solution.

Thanks :)
 
Am 08.07.2010 22:24, schrieb Mr. X.:
I don't know how much columns will be on first time, and I want dynamically
to change it (I don't know if there are two, or ten,
so {firstColumn, secondColumn} may not be the best solution.

You can always assign a new array to the PrimaryKey property.
 
The situation :
I am doing a loop, and each time I can add only one element to the
primaryKey,
so I must resize the array, without clearing it, but I don't know how to do
that.

Thanks :)
 
Am 08.07.2010 22:51, schrieb Mr. X.:
The situation :
I am doing a loop, and each time I can add only one element to the
primaryKey,
so I must resize the array, without clearing it, but I don't know how to do
that.

In the loop, collect the columns in a List(Of DataColumn).
After the loop, set the PrimaryKey to YourList.ToArray.
 
Thanks :)

Armin Zingler said:
Am 08.07.2010 22:51, schrieb Mr. X.:

In the loop, collect the columns in a List(Of DataColumn).
After the loop, set the PrimaryKey to YourList.ToArray.
 
One little problem.
MyTable.PrimaryKey = MyList.toArray
I got the exception : column must belong to a table.
 
.... That’s even I have checked, and the value on the list, is the column.
When inserting to the list I did :
MyList.add(myKey, myTable.columns(myKey).dataType())
 
Am 09.07.2010 12:00, schrieb Mr. X.:
... That’s even I have checked, and the value on the list, is the column.
When inserting to the list I did :
MyList.add(myKey, myTable.columns(myKey).dataType())

Sorry?
Yes, the column must belong to the table, and you must
add myTable.columns(myKey), not it's dataType property.
 
Back
Top