AdBlock Detected

We provide high-quality source code for free. Please consider disabling your AdBlocker to support our work.

Buy me a Coffee

Saved Tutorials

No saved posts yet.

Press Enter to see all results

How to change a DataColumn Type in .NET/C#

By pushpam abhishek
Listen to this article

How to change a DataColumn Type in .NET/C#

In some situations, you may need to change the DataColumn type of a DataTable. You can easily do so when the DataTable is empty. On the contrary, you are stuck! Fortunately in that case there is a way, albeit a circonvoluted one: clone the DataTable schema, change the type and copy the data with the ImportRow method like this (in this example we want to change the TimeStamp string type to DateTime type):

How to change a DataColumn Type in .NET/C#



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.OleDb;
using System.IO;

namespace How_to_change_a_DataColumn_Type
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable dataTable;   
DataColumn dataColumn;

DataRow row;       

dataTable = new DataTable("Contact");

dataColumn = new DataColumn("Id");

dataColumn.DataType = typeof(int);

dataColumn.AutoIncrement = true;

dataTable.Columns.Add(dataColumn);

dataColumn = new DataColumn("Name");

dataTable.Columns.Add(dataColumn);

 

dataColumn = new DataColumn("TimeStamp");

dataColumn.DataType = typeof(string);

dataTable.Columns.Add(dataColumn);             

dataColumn = new DataColumn("Category");

dataColumn.DataType = typeof(int);

dataTable.Columns.Add(dataColumn); 

// Primary Key

dataTable.PrimaryKey = new DataColumn[] { dataTable.Columns["Id"] };       

row = dataTable.NewRow();

row[dataTable.Columns[1]] = "John";

row[dataTable.Columns[2]] = DateTime.Now;

row[dataTable.Columns[3]] = 0;

dataTable.Rows.Add(row);

 

row = dataTable.NewRow();

row[dataTable.Columns[1]] = "Jane";

row[dataTable.Columns[2]] = DateTime.Now;

row[dataTable.Columns[3]] = 0;

dataTable.Rows.Add(row);

 

row = dataTable.NewRow();

row[dataTable.Columns[1]] = "Joe";

row[dataTable.Columns[2]] = DateTime.Now;

row[dataTable.Columns[3]] = 1;

dataTable.Rows.Add(row);

foreach (DataRow dataRow in dataTable.Rows) {

foreach (DataColumn dt in dataTable.Columns) {

Console.WriteLine(dataRow[dt].ToString());

}

}      

 

DataTable dataTableClone = dataTable.Clone();

foreach (DataColumn dt in dataTableClone.Columns) {

Console.WriteLine(dt.ColumnName.ToString());

Console.WriteLine(dt.DataType.ToString());

}

Console.WriteLine("============");

dataTableClone.Columns["TimeStamp"].DataType = typeof(DateTime);

foreach (DataColumn dt in dataTableClone.Columns) {

Console.WriteLine(dt.ColumnName.ToString());

Console.WriteLine(dt.DataType.ToString());

}

Console.WriteLine("============");

foreach (DataRow dr in dataTable.Rows) {

dataTableClone.ImportRow(dr);

}

foreach (DataRow dataRow in dataTableClone.Rows) {

foreach (DataColumn dt in dataTableClone.Columns) {

Console.WriteLine(dataRow[dt].ToString());

}

 

}

DataRow[] foundRows;

string filter = "TimeStamp < " + "'" + DateTime.Now.AddDays(1) + "'";

Console.WriteLine(filter);

foundRows = dataTableClone.Select(filter);



for(int i = 0; i < foundRows.Length; i ++) {

Console.WriteLine(foundRows[i][1]);
Console.ReadLine();

}

 

}
        }
    }


Share this post

pushpam abhishek

About pushpam abhishek

Pushpam Abhishek is a Software & web developer and designer who specializes in back-end as well as front-end development. If you'd like to connect with him, follow him on Twitter as @pushpambhshk

Comments