Saturday, March 24, 2012

Paging Problem in Dynamically added GridView

Hi

I am developing a composite control which includes a GridView. GridView is created and bound in CreateChildControls() method. Normally the control works greate. But if I set AllowPaging=True, it gives an "Object null reference exception". Following is the code. Please give me a solution. [N:B: I have added the PageIndexChages Event also].

protected override void CreateChildControls(){_lsAjaxGridView =new GridView();_lsAjaxGridView.ID ="Leadsoft_AjaxGrid";_lsAjaxGridView.AutoGenerateColumns =this.AutoGenerateColumns;foreach (DataControlField dataControlFieldin Columns) _lsAjaxGridView.Columns.Add(dataControlField);if (this.AllowPaging){if (this.PageSize > 0) _lsAjaxGridView.PageSize =this.PageSize; _lsAjaxGridView.AllowPaging =this.AllowPaging; _lsAjaxGridView.PageIndexChanging +=new GridViewPageEventHandler(Leadsoft_AjaxGrid_PageIndexChanged);}_lsAjaxGridView.DataSource = LSDataSourceView;_lsAjaxGridView.DataBind();}
The exception occurs in "_lsAjaxGridView.DataBind();" only when AllowPaging is True.
Exception Message:"Object reference not set to an instance of an object."

Hi this problem is solved. thing is any DataBinding should be done after the control is added to the form.

lsAjaxGridView.DataSource = LSDataSourceView;

Controls.Add(lsAjaxGridView);
_lsAjaxGridView.DataBind();

No comments:

Post a Comment