I have:
DataTable Table = new DataTable;
SqlConnection = new System.Data.SqlClient.SqlConnection("Data Source=" + ServerName + ";Initial Catalog=" + DatabaseName + ";Integrated Security=SSPI; Connect Timeout=120");
SqlDataAdapter adapter = new SqlDataAdapter("Select * from " + TableName, Connection);
adapter.FillSchema(Table, SchemaType.Source);
adapter.Fill(Table);
DataColumn column = DataTable.Columns[0];
What I want to do is:
Assume currently column.DataType.Name is "Double". I want it to become "Int32".
How do I achieve this?
if you want to change only a column.for example from string to int32 you can use expression.
You cannot change the DataType after the Datatable is filled with data. However, you can clone the Data table, change the column type and load data from previous data table to the cloned table as shown below.
Old post, but I thought I'd weigh in, with a DataTable extension that can convert a single column at a time, to a given type:
It can then be called like this:
Of course using whatever type you desire, as long as each value in the column can actually be converted to the new type.