R R R R - 26 days ago 12
ASP.NET (C#) Question

Page size causing error in gridview DataBind

I am facing a strange issue or maybe i am doing a silly mistake.
I have a datatable which contains five rows.
I am creating a gridview from codebehind.
for pagination if i specify

Page size =5
(the size of datatable) everything is fine but when i specify the page size less than the size of the datatable e.g.
Page size=2
,
I am getting :

Object reference not set to an instance of an object.

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


StackTrace:

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


Any help would be appreciated.

Thanks in advance.

Answer

This is caused because no Pager Template is set to your GridView. I have modified your code to get you started. First add RowDataBound handler and set your Pager:

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

And here is basic Pager control:

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

This will do the job.