我正面临一个奇怪的问题,或者我可能犯了一个愚蠢的错误。我有一个包含五行的数据表。我正在从代码隐藏创建一个网格视图。对于分页,如果我指定Page size =5
(数据表的大小)一切都很好,但是当我指定的页面大小小于数据表的大小时,例如Page size=2
,我得到:
你调用的对象是空的。
在gvEmployee.DataBind();
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace demogrid
{
public partial class grid : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable table = new DataTable();
table.Columns.Add("Dosage", typeof(int));
table.Columns.Add("Drug", typeof(string));
table.Columns.Add("Patient", typeof(string));
table.Columns.Add("Date", typeof(DateTime));
// Here we add five DataRows.
table.Rows.Add(25, "Indocin", "David", DateTime.Now);
table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
BindData(table);
}
}
protected void BindData(DataTable dt)
{
GridView gvEmployee = new GridView();
gvEmployee.AllowPaging = true;
gvEmployee.PageSize = 5;//getting error if the page size is less than 5
gvEmployee.AutoGenerateColumns = false;
if (dt != null)
{
for (int i = 0; i < dt.Columns.Count; i++)
{
BoundField boundfield = new BoundField();
boundfield.DataField = dt.Columns[i].ColumnName.ToString();
boundfield.HeaderText = dt.Columns[i].ColumnName.ToString();
gvEmployee.Columns.Add(boundfield);
}
gvEmployee.DataSource = dt;
gvEmployee.DataBind();
gvEmployee.Width = 600;
gvEmployee.HeaderStyle.CssClass = "header";
gvEmployee.RowStyle.CssClass = "rowstyle";
Panel1.Controls.Add(gvEmployee);
}
}
}
}
堆栈跟踪:
at System.Web.UI.WebControls.GridView.get_StateFormatter()
at System.Web.UI.WebControls.GridView.BuildCallbackArgument(Int32 pageIndex)
at System.Web.UI.WebControls.GridView.CreateNumericPager(TableRow row, PagedDataSource pagedDataSource, Boolean addFirstLastPageButtons)
at System.Web.UI.WebControls.GridView.InitializePager(GridViewRow row, Int32 columnSpan, PagedDataSource pagedDataSource)
at System.Web.UI.WebControls.GridView.CreateRow(Int32 rowIndex, Int32 dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, Boolean dataBind, Object dataItem, DataControlField[] fields, TableRowCollection rows, PagedDataSource pagedDataSource)
at System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding)
at System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data)
at System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data)
at System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data)
at System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback)
at System.Web.UI.WebControls.DataBoundControl.PerformSelect()
at System.Web.UI.WebControls.BaseDataBoundControl.DataBind()
at System.Web.UI.WebControls.GridView.DataBind()
at demogrid.grid.BindData(DataTable dt) in c:\Users\sample\Documents\Visual Studio 2012\Projects\demogrid\demogrid\grid.aspx.cs:line 59
at demogrid.grid.Page_Load(Object sender, EventArgs e) in c:\Users\sample\Documents\Visual Studio 2012\Projects\demogrid\demogrid\grid.aspx.cs:line 32
任何帮助,将不胜感激。
提前致谢。