views:

247

answers:

2

How do you convert a DataColumn of GUIDs set as type string to a DataColumn of GUIDs where the type is Guid? A column in the source data has the wrong type and I cannot change it there.

+3  A: 

Once the datatable has been populated with data, you can't change the column type. There's a few approaches you can take:

  1. If you're using a DataAdapter to fill the table, you can use DataAdapter.FillSchema() to get the structure of the table first. Then you can modify the column type, and then fill it with data.

  2. Take the initial table and clone it, change the column type on the new table, and then use DataTable.ImportRow() to import each row from the old table.

  3. Create a new column, copy the values from the old column to the new column, and delete the old column.

womp
A: 

You can add to womp's answer the following option:

  1. After you get the table full of the data you can add a new DataColumn to the Columns of the data table this way:

    DataColumn dc = new DataColumn("GUIDColumnName",typeof(Guid),"StringColumnName"); DataTableObj.Columns.Add(dc);

this will not hurt performance if your data volume was huge.

SubPortal