DataSet does not support System.Nullable<> in Export

前端 未结 4 918
暖寄归人
暖寄归人 2021-01-31 01:34

I was trying to generate a Report using Export to Excell, PDF, TextFile. Well I am doing this in MVC. I have a class which I named SPBatch (which is the exact name of my Stored

4条回答
  •  南旧
    南旧 (楼主)
    2021-01-31 02:15

    I would search for nullable and replace it with a string which can be null unlike a DateTime.

    foreach (PropertyInfo pi in properties)
    {
        if (pi.PropertyType.Name.Contains("Nullable"))
           myDataType = typeof(String);
        else
           myDataType = pi.PropertyType;
    }
    

    Here is a complete version:

    private DataTable CreateDataTable(PropertyInfo[] properties)
    {
        DataTable dt = new DataTable();
        DataColumn dc = null;
        foreach (PropertyInfo pi in properties)
        {
            dc = new DataColumn();
            dc.ColumnName = pi.Name;
    
            if (pi.PropertyType.Name.Contains("Nullable"))
                dc.DataType = typeof(String);
            else
                dc.DataType = pi.PropertyType;
    
            // dc.DataType = pi.PropertyType;
            dt.Columns.Add(dc);
        }
        return dt;
    }
    

提交回复
热议问题