Ms.*_*. B 47 c# nullable dataset export-to-excel asp.net-mvc-4
我试图使用Export to 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; }
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我的一些列被声明为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);
}
}
Run Code Online (Sandbox Code Playgroud)
错误显示在带注释的错误上.你能帮帮我怎么办?我尝试在我的代码中添加DBNull但仍然得到相同的错误.我尝试在我的SPBatch中删除Nullable但是我得到一个错误,一些表需要声明为Nullable.
我该怎么办?
Dam*_*ith 116
尝试
dt.Columns.Add(pi.Name, Nullable.GetUnderlyingType(
pi.PropertyType) ?? pi.PropertyType);
Run Code Online (Sandbox Code Playgroud)
由于生成数据表的 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
34283 次 |
| 最近记录: |