繁体   English   中英

DataSet 不支持导出中的 System.Nullable<>

[英]DataSet does not support System.Nullable<> in Export

我试图使用导出到 Excell、PDF、TextFile 生成报告。 嗯,我在 MVC 中这样做。 我有一个名为 SPBatch 的类(它是我的 SQL 中存储过程的确切名称),它包含以下内容:

public string BatchNo { get; set; }
public string ProviderName { get; set; }
public Nullable<System.Int32> NoOfClaims { get; set; }
public Nullable<System.Int32> TotalNoOfClaims { get; set; }
public Nullable<System.Decimal> TotalBilled { get; set; }
public Nullable<System.Decimal> TotalInputtedBill { get; set; }
public Nullable<System.DateTime> DateCreated { get; set; }
public Nullable<System.DateTime> DateSubmitted { get; set; }
public Nullable<System.DateTime> DueDate { get; set; }
public string Status { get; set; }
public string RefNo { get; set; }
public string BatchStatus { get; set; }
public string ClaimType { get; set; }

正如您所看到的,我的一些列被声明为 Nullable。 从搜索结果并将结果显示在表格中,它进行得很顺利。 我下面有几个按钮是用于导出的图像按钮,每次我尝试在 Excel 中导出时,我总是在我的代码的这一部分遇到问题“ DataSet 不支持 System.Nullable<> ”:

foreach (MemberInfo mi in miArray)
{
    if (mi.MemberType == MemberTypes.Property)
    {
        PropertyInfo pi = mi as PropertyInfo;
        dt.Columns.Add(pi.Name, pi.PropertyType); //where the error pop's up.

    }
    else if (mi.MemberType == MemberTypes.Field)
    {
        FieldInfo fi = mi as FieldInfo;
        dt.Columns.Add(fi.Name, fi.FieldType);
    }
}

错误显示在带有注释的那个上。 你能帮我做什么吗? 我尝试在我的代码中添加 DBNull,但仍然出现相同的错误。 我尝试在我的 SPBatch 中删除 Nullable,但我收到一个错误,指出需要将某些表声明为 Nullable。

我该怎么办?

尝试

dt.Columns.Add(pi.Name, Nullable.GetUnderlyingType(
            pi.PropertyType) ?? pi.PropertyType);

由于生成数据表的 C# 版本和一些黑客攻击,我可以在 VB 中提供这个答案 - 我把它放在这里是因为我在使用时想要从存储的过程中获取可过滤的数据集有很多麻烦一个简单的数据层。 我希望它可以帮助别人!

注意:用例是您希望使用 BindingSource.Filter = "some query string" 的地方:

Imports System.Reflection

Public Module Extenders
<System.Runtime.CompilerServices.Extension>
Public Function ToDataTable(Of T)(collection As IEnumerable(Of T), tableName As String) As DataTable
    Dim tbl As DataTable = ToDataTable(collection)
    tbl.TableName = tableName
    Return tbl
End Function

<System.Runtime.CompilerServices.Extension>
Public Function ToDataTable(Of T)(collection As IEnumerable(Of T)) As DataTable
    Dim dt As New DataTable()

    Dim tt As Type = GetType(T)
    Dim pia As PropertyInfo() = tt.GetProperties()

    'Create the columns in the DataTable

    For Each pi As PropertyInfo In pia
        Dim a = 
If(Nullable.GetUnderlyingType(pi.PropertyType), pi.PropertyType)
        dt.Columns.Add(pi.Name, If(Nullable.GetUnderlyingType(pi.PropertyType), pi.PropertyType))
    Next

    'Populate the table

    For Each item As T In collection
        Dim dr As DataRow = dt.NewRow()
        dr.BeginEdit()

        For Each pi As PropertyInfo In pia

            dr(pi.Name) = If(Nullable.GetUnderlyingType(pi.PropertyType) Is GetType(DateTime), DBNull.Value, pi.GetValue(item, Nothing))
        Next
        dr.EndEdit()
        dt.Rows.Add(dr)
    Next
    Return dt
End Function

End Module

我会搜索 nullable 并将其替换为一个字符串,该字符串与 DateTime 不同,该字符串可以为 null。

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

这是一个完整的版本:

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;
}

1) 定义这个扩展如下

public static class ListExtensions
    {
        public static DataTable ToDataTable<T>(this List<T> list)
        {
            DataTable table = new DataTable(typeof(T).Name);

            //Get Properites of List Fiels
            PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);

            //Create Columns as Fields of List
            foreach (PropertyInfo propertyInfo in props)
            {
                var column = new DataColumn
                {
                    ColumnName = propertyInfo.Name,
                    DataType = propertyInfo.PropertyType.Name.Contains("Nullable") ? typeof(string) : propertyInfo.PropertyType
                };

                table.Columns.Add(column);
            }

            //Fill DataTable with Rows of List
            foreach (var item in list)
            {
                var values = new object[props.Length];

                for (var i = 0; i < props.Length; i++)
                {
                    values[i] = props[i].GetValue(item, null);
                }

                table.Rows.Add(values);
            }


            return table;
        }
    }

2) 调用扩展方法如下,其中 [_lstOperationDetails] 是我们想要将其从 List 转换为 DataTable 的 List

DataTable operationDetails = _lstOperationDetails.ToDataTable();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM