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.
Once the datatable has been populated with data, you can't change the column type. There's a few approaches you can take:
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.
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.
Create a new column, copy the values from the old column to the new column, and delete the old column.
You can add to womp's answer the following option:
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 have a performance hit if your data volume is huge.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With