🙂 İNSANLARIN EN HAYIRLISI INSANLARA FAYDALI OLANDIR 🙂

Ramazan HABER / C# RMOS / Convert Column Type Column tipini değiştirme veri varsa bile

1-) C# RMOS - Convert Column Type Column tipini değiştirme veri varsa bile

 

public static class DataTableExt

    {

        public static void ConvertColumnType(this DataTable dt, string columnName, Type newType)

        {

            using (DataColumn dc = new DataColumn(columnName + "_new", newType))

            {

                // Add the new column which has the new type, and move it to the ordinal of the old column

                int ordinal = dt.Columns[columnName].Ordinal;

                dt.Columns.Add(dc);

                dc.SetOrdinal(ordinal);

 

                // Get and convert the values of the old column, and insert them into the new

                foreach (DataRow dr in dt.Rows)

                    dr[dc.ColumnName] = Convert.ChangeType(dr[columnName], newType);

 

                // Remove the old column

                dt.Columns.Remove(columnName);

 

                // Give the new column the old column's name

                dc.ColumnName = columnName;

            }

        }

    }

 

 KULLANIMI

 

 dtHesap.ConvertColumnType("Rsat_Tutar", typeof(string));

 

 

 

kaynak : https://stackoverflow.com/questions/9028029/how-to-change-datatype-of-a-datacolumn-in-a-datatable

 

 2021 Kasım 10 Çarşamba
 508