页面大小导致gridview DataBind中的错误

时间:2016-11-11 16:11:24

标签: c# asp.net gridview pagertemplate

我正面临一个奇怪的问题,或者我犯的是一个愚蠢的错误。 我有一个包含五行的数据表。 我正在从codebehind创建gridview。 如果我指定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

任何帮助都将不胜感激。

提前致谢。

2 个答案:

答案 0 :(得分:2)


发生异常是因为您的 GridView 尚未成为PAGE的控件而您正在调用 DataBind 您必须在数据绑定之前在页面上添加控件
您还必须实现 PageIndexChanging 事件来处理Page Changing Event。

还有一件事是你需要从 if(!IsPostBack)块移动你的数据表代码,因为当页面POSTBACK和控件不再在页面上时它不会被执行更改页面或触发任何POSTBACK事件时。 这是一个适合您的工作代码。

seek

答案 1 :(得分:1)

这是因为没有为您的GridView设置寻呼机模板。 我已修改您的代码以帮助您入门。首先添加RowDataBound处理程序并设置寻呼机:

    protected void BindData(DataTable dt)
    {
        GridView gvEmployee = new GridView();
        gvEmployee.AllowPaging = true;
        gvEmployee.PageSize = 4;//this Will work now
        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.RowDataBound += gvEmployee_RowDataBound;
            gvEmployee.PagerTemplate = new MyTemplate();
            gvEmployee.DataBind();
            gvEmployee.Width = 600;
            gvEmployee.HeaderStyle.CssClass = "header";
            gvEmployee.RowStyle.CssClass = "rowstyle";

            Panel1.Controls.Add(gvEmployee);
        }
    }

    void gvEmployee_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        switch (e.Row.RowType)
        {
            case DataControlRowType.DataRow:
                break;
            case DataControlRowType.EmptyDataRow:
                break;
            case DataControlRowType.Footer:
                break;
            case DataControlRowType.Header:
                break;
            case DataControlRowType.Pager:
                //TODO: handle your pager...
                break;
            case DataControlRowType.Separator:
                break;
            default:
                break;
        }
    }

这是基本的寻呼机控制:

public class MyTemplate : WebControl, ITemplate
{
    public void InstantiateIn(Control container)
    {
        //TODO: create your pager controls...
    }
}

这将完成这项工作。