The example in the MSDN documentation for DataTable.PrimaryKey should help: msdn.microsoft.com/.../system.data.datatable.primarykey(v=vs.110).aspx.
Instead of creating the columns in the table, you would read them instead and put them into a list. The set the PrimaryKey property to the list containing the columns that you want to be the primary keys. Be sure you see the important note in the AF SDK documentation for the AFTable.Table property about modifying the table's schema:
"When modifying the table's schema (e.g. adding a column or changing a column name), you must first call CheckOut() to check out the object and ensure you are modifying the latest copy of the table. When finished, call AFTable.Table.AcceptChanges to commit the schema changes. Changes to the table schema cannot be automatically detected like changes to other properties."
Thank you. I was able to make it work with the following:
Dim PrimaryKeyColumnArray(1) As DataColumn
PrimaryKeyColumnArray(0) = MyDatabase.Tables(LocalTableIndex).Table.Columns(1) 'Column index=1 is the 2nd column.
MyDatabase.Tables(LocalTableIndex).Table.PrimaryKey = PrimaryKeyColumnArray
Now, I need to know how to make the Column NO LONGER a Primary Key. Does anyone know how to do that? Thank you
Could you please try: DataTable.PrimaryKey = null;?
Let us know if it works for you!
The code did not like that. The code accepted .PrimaryKey = Nothing, but that did not seem to do the trick.
The good news is that I don't need this anymore. Formerly, I was doing this:
1. Clear the table
2. Add an empty row
3. Add data to the new empty row.
Step 2 was throwing an exception because PrimaryKey cannot contain null
So, I revised the code as follows:
2. Add the row WITH the PrimaryKey data
3. Add additional data for the row.
Retrieving data ...