我遇到了将数据表绑定到自定义gridview控件的问题。我正在遇到的问题是当我对控件进行数据绑定时的无引用异常。
当数据源是由SqlDataAdapter填充的数据表时,gridview绑定没有问题。这是适用于我的代码:
public override void Load(DateTime startDate, DateTime endDate)
{
using (DataTable dt = new DataTable())
{
using (dbCheckpointWarehouseDataContext thisDataContext = new dbCheckpointWarehouseDataContext())
{
var qryItemCount = [LINQ QUERY THAT WORKS FINE.]
using (SqlCommand cmd = thisDataContext.GetCommand(qryItemCount) as SqlCommand)
{
using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
{
adapter.Fill(dt);
}
if (dt.Rows.Count > 0)
{
CustomGridView cgvResults = new CustomGridView();
cgvResults.AddBoundField("DistrictManagerName", "District Manager", "");
cgvResults.AddBoundField("newMetric", "newMetric", "");
cgvResults.DataSource = dt;
cgvResults.DataBind();
m_ReportHTMLString = Utilities.RenderHTMLStringOfControl(cgvResults);
}
}
}
}
}
如果我尝试使用强类型数据表加载相同的自定义控件,我会得到空引用异常。有三行注释掉了我将数据表绑定到stock gridview控件的位置。这也有效。当我使用带有自定义gridview的强类型数据表时,我只会遇到这个问题。异常是在DataBind()方法中生成的。在调试模式下,我已经验证了数据表不为空,并且在设置DataSource属性时没有异常。以下是生成空引用的代码:
public override void Load(DateTime startDate, DateTime endDate)
{
using (DataTable dt = new DataTable())
{
dt.Columns.Add("DistrictManagerName", typeof(string));
dt.Columns.Add("newMetric", typeof(Int32));
DataRow row;
using (dbCheckpointWarehouseDataContext thisDataContext = new dbCheckpointWarehouseDataContext())
{
var qryItemCount = [LINQ QUERY THAT WORKS FINE.]
.AsEnumerable()
.Select([SELECT STATEMENT THAT WORKS FINE])
foreach (var rowtrans in qryTest)
{
row = dt.NewRow();
row["DistrictManagerName"] = rowtrans.DistrictManagerName;
row["newMetric"] = Convert.ToInt32(rowtrans.newMetric);
dt.Rows.Add(row);
}
string total = dt.Compute("Sum(newMetric)", "").ToString();
row = dt.NewRow();
row["DistrictManagerName"] = "Total";
row["newMetric"] = String.Format("{0:#,0}", total);
dt.Rows.Add(row);
dt.AcceptChanges();
CustomGridView cgvResults = new CustomGridView();
cgvResults.AddBoundField("", "District Manager", "");
cgvResults.AddBoundField("", "Total", "");
cgvResults.DataSource = dt;
cgvResults.DataBind();
//GridView cgvResults = new GridView();
//cgvResults.DataSource = dt.DefaultView;
//cgvResults.DataBind();
m_ReportHTMLString = Utilities.RenderHTMLStringOfControl(cgvResults);
}
}
}
以下是自定义gridview控件的代码:
public CustomGridView() : base()
{
AutoGenerateColumns = true;
AllowPaging = true;
AllowSorting = true;
PageSize = 25;
GridLines = GridLines.Vertical;
CssClass = "gridView";
HeaderStyle.CssClass = "gridViewHeader";
RowStyle.CssClass = "gridViewRow";
AlternatingRowStyle.CssClass = "gridViewAlternatingRow";
PagerStyle.CssClass = "gridViewPager";
CellPadding = 3;
}
public void AddBoundField(string dataField, string headerText, string sortExpression)
{
BoundField bf = new BoundField();
if (dataField != "")
{
bf.DataField = dataField;
}
if (headerText != "")
{
bf.HeaderText = headerText;
}
if (sortExpression != "")
{
bf.SortExpression = sortExpression;
}
Columns.Add(bf);
}
感谢您提供任何帮助。
答案 0 :(得分:0)
我找到了解决方案。我更改了设置:
AllowPaging = true
到
AllowPaging = false
我的自定义控件现在可以正常运行。
我希望有人能够解释为什么此设置会在强类型数据集上导致NullReference异常。