My Simple World

access new world!


10 months ago · 1 MIN READ
#Working  #Learning 


Changing data type of datatable after get filled is not possible but one of the work abound for this is clone datatable and change type

First way

//clone datatable     
DataTable dtCloned = dt.Clone();
//change data type of column
dtCloned.Columns[0].DataType = typeof(Int32);
//import row to cloned datatable
foreach (DataRow row in dt.Rows) 

second way

and other way to do it is like this , fill schema first - than change column datatype and than fill datable

adapter.FillSchema(table, SchemaType.Source);
table.Columns[0].DataType = typeof (Int32);

third way

one more way to do it is

System.Data.DataTable dt = new System.Data.DataTable();
dt.Columns.Add("Col1", typeof(int));

System.Data.DataTable dt2 = new System.Data.DataTable();
dt2.Columns.Add("Col1", typeof(string));
dt2.Load(dt.CreateDataReader(), System.Data.LoadOption.OverwriteChanges);



neo wing

Proudly powered by Canvas · Sign In