51
DataTable dt = new DataTable();  
dt.Columns.Add(new DataColumn(gridColumn1, typeof(bool)));

I was expecting the result of the below line to include info about the DataColumns Type (bool):

?dt.Columns[0].GetType()
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321

5 Answers5

96

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

15

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

using System.Data;

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

or using column name :

if (dt.Columns["yourColumnName"].DataType == typeof(DateTime))
{
    //...
}
Diego Montania
  • 322
  • 5
  • 12
VDWWD
  • 35,079
  • 22
  • 62
  • 79
10
dt.Columns[0].DataType.Name.ToString()
FelixSFD
  • 6,052
  • 10
  • 43
  • 117
0

You can get column type of DataTable with DataType attribute of datatable column like below:

var type = dt.Columns[0].DataType

dt: DataTable object.

0: DataTable column index.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Arpit Trivedi
  • 121
  • 2
  • 7
-2
if (dr[dc.ColumnName].GetType().ToString() == "System.DateTime")
General Grievance
  • 4,555
  • 31
  • 31
  • 45