Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a DataTable Columns DataType

People also ask

How to get DataTable column DataType in c#?

You can get the column data type of the data table easily. There are just two lines of code, that will help you find the data type. You can get the column data type of the data table easily. There are just two lines of code, that will help you find the data type.

What are DataTable columns?

Description. The columns option in the initialisation parameter allows you to define details about the way individual columns behave. For a full list of column options that can be set, please see the related parameters below.

How can we change the DataType of a column in DataTable dynamically in C#?

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.


What you want to use is this property:

dt.Columns[0].DataType

The DataType property will set to one of the following:

Boolean
Byte
Char
DateTime
Decimal
Double
Int16
Int32
Int64
SByte
Single
String
TimeSpan
UInt16
UInt32
UInt64

DataColumn.DataType Property MSDN Reference


You could always use typeof in the if statement. It is better than working with string values like the answer of Natarajan.

if (dt.Columns[0].DataType == typeof(DateTime))
{
}

dt.Columns[0].DataType.Name.ToString()