How can I programmaticaly add a column to a key.

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 :)
 
A

Armin Zingler

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.
 
M

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?

Thanks :)
 
A

Armin Zingler

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.
 
M

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.

Thanks :)
 
A

Armin Zingler

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.
 
M

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.

Thanks :)
 
A

Armin Zingler

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.
 
M

Mr. X.

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.
 
M

Mr. X.

One little problem.
MyTable.PrimaryKey = MyList.toArray
I got the exception : column must belong to a table.
 
M

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())
 
A

Armin Zingler

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.
 

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