Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Modifying columns in a datatable dynamically?

Status
Not open for further replies.

mcwadar

Programmer
Dec 30, 2002
2
US
I am trying to Add/Delete/Change columns in a datatable depending on the rows in another datatable. For example: One table has rows that contain information about fields such as Name, Phone, Address. I am trying to update the columns in another table (existing or new) to be Name, Phone, Address. Then if a user wants to add more information such as Email. The first table would add another row (i can do this), and the second table would add another column (need help). I accomplished this using VB6 a few months back, but have recently upgraded to VB.Net and havn't found a method of doing this. Any help would be greatly appreciated.
 
I forgot to mention the database is in Access 2000.
 
You can use a DataTable object's DataColumnCollection to add/remove columns. However, if you're also using a dataset, then you'll have to update the schema also with something like WriteXmlSchema()

Hopefully this will get you pointed in the right direction.

Dim cols As DataColumnCollection = myTable.Columns
Dim myColumn As DataColumn = New DataColumn
myColumn.DataType = System.Type.GetType("System.Decimal")
myColumn.AllowDBNull = False
myColumn.Caption = "Caption"
myColumn.ColumnName = "Name"
myColumn.DefaultValue = 1
myTable.Columns.Add(myColumn)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top