Get a DataTable Columns DataType

前端 未结 5 1106
萌比男神i
萌比男神i 2021-01-31 14:49
DataTable dt = new DataTable();  
dt.Columns.Add(new DataColumn(gridColumn1, typeof(bool)));

I was expecting the result of the below line to include in

相关标签:
5条回答
  • 2021-01-31 15:23

    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))
    {
    }
    
    0 讨论(0)
  • 2021-01-31 15:26

    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

    0 讨论(0)
  • 2021-01-31 15:27

    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.

    Hope It Helps

    Ty :)

    0 讨论(0)
  • 2021-01-31 15:27
    dt.Columns[0].DataType.Name.ToString()
    
    0 讨论(0)
  • 2021-01-31 15:29

    if (dr[dc.ColumnName].GetType().ToString() == "System.DateTime")

    0 讨论(0)
提交回复
热议问题