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.
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.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With