471,079 Members | 976 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,079 software developers and data experts.

GridVew Error, Object reference not set to an instance of an object.

My page has several GridView controls placed on it from the designer.
My datasource is dynamic (DataTable) assigned in the code behide.

On this same page I am creating a GridView control from the code behide and placing it in an asp:table, assigning the properties and styles to duplicate the designer GridViews. Everything works, displays good and column sorting works.

However. If the datasource contains more records then the GridView PageSize I receive the Object reference error. I have tried everything I can think of to no avail. If my datasource contains 30 records and my page size is default 10 or anything less than 30 I will receive the error. If I change the page size to 30 or greater it works fine (No pageing of course) The boolean AllowPaging is set to true. The paging on the GridViews placed on the page from the designer works fine. Just the dynamic GridView causes the error. The datasource for all of the GirdViews are obtained and assigned in the same fashion.

Any suggestions would be greatly appreciated.
May 18 '07 #1
2 7223
Frinavale
9,735 Expert Mod 8TB
My page has several GridView controls placed on it from the designer.
My datasource is dynamic (DataTable) assigned in the code behide.

On this same page I am creating a GridView control from the code behide and placing it in an asp:table, assigning the properties and styles to duplicate the designer GridViews. Everything works, displays good and column sorting works.

However. If the datasource contains more records then the GridView PageSize I receive the Object reference error. I have tried everything I can think of to no avail. If my datasource contains 30 records and my page size is default 10 or anything less than 30 I will receive the error. If I change the page size to 30 or greater it works fine (No pageing of course) The boolean AllowPaging is set to true. The paging on the GridViews placed on the page from the designer works fine. Just the dynamic GridView causes the error. The datasource for all of the GirdViews are obtained and assigned in the same fashion.

Any suggestions would be greatly appreciated.
You've likely tried this already but have you set the GridView's paging property to allow paging?

Edit: Errr...sorry should have read the whole message...This sounds a bit crazy.

Double check the allow paging again though just to be sure.

Could you post the code that creates your GridView and data source please?
May 18 '07 #2
Data Source for the GridView is a System.Data.DataTable with proper field names and types.

As soon as the gv.DataBind() is executed I receive the Error.
The gvInvoices_DataBound event does not fire.

GridView Source:
Expand|Select|Wrap|Line Numbers
  1.       gv = new System.Web.UI.WebControls.GridView();
  2.       gv.ID = "gvInvoice" + aPoints[i];
  3.       gv.AllowPaging = true;
  4.       gv.AllowSorting = true;
  5.       gv.AutoGenerateColumns = false;
  6.       gv.BackColor = System.Drawing.Color.White;
  7.       gv.BorderColor = System.Drawing.Color.FromArgb(231,231,255);
  8.       gv.BorderStyle = System.Web.UI.WebControls.BorderStyle.None;
  9.       gv.BorderWidth = new System.Web.UI.WebControls.Unit("1px");
  10.       gv.CellPadding = 3;
  11.       gv.ShowFooter = true;
  12.       gv.PageSize = 50;
  13.       // GridView Columns
  14.       gvc = new System.Web.UI.WebControls.BoundField();
  15.       gvc.DataField = "invoice_number";
  16.       gvc.HeaderText = "Number";
  17.       gvc.ReadOnly = true;
  18.       gvc.SortExpression = "number";
  19.       gvc.ItemStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
  20.       gv.Columns.Add(gvc);
  21.       gvc = new System.Web.UI.WebControls.BoundField();
  22.       gvc.DataField = "amount";
  23.       gvc.HeaderText = "Amount";
  24.       gvc.ReadOnly = true;
  25.       gvc.SortExpression = "amount";
  26.       gvc.ItemStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
  27.       gv.Columns.Add(gvc);
  28.       // Grid View Styles
  29.       gv.RowStyle.BackColor = System.Drawing.Color.FromArgb(231,231,255);
  30.       gv.RowStyle.ForeColor = System.Drawing.Color.FromArgb(74,60,140);
  31.       gv.RowStyle.Font.Names = new string[] { "Tahoma","Arial","Verdana","Trebuchet","Sans-Serif" };
  32.       gv.RowStyle.Font.Size = new System.Web.UI.WebControls.FontUnit("8pt");
  33.       gv.SelectedRowStyle.BackColor = System.Drawing.Color.FromArgb(115,138,156);
  34.       gv.SelectedRowStyle.Font.Bold = true;
  35.       gv.SelectedRowStyle.ForeColor = System.Drawing.Color.FromArgb(247,247,247);
  36.       gv.PagerStyle.BackColor = System.Drawing.Color.FromArgb(231,231,255);
  37.       gv.PagerStyle.ForeColor = System.Drawing.Color.FromArgb(74,60,140);
  38.       gv.PagerStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Right;
  39.       gv.PagerStyle.Font.Names = new string[] { "Tahoma","Arial","Verdana","Trebuchet","Sans-Serif" };
  40.       gv.PagerStyle.Font.Size = new System.Web.UI.WebControls.FontUnit("9pt");
  41.       gv.HeaderStyle.ForeColor = System.Drawing.Color.FromArgb(247,247,247);
  42.       gv.HeaderStyle.BackColor = System.Drawing.Color.FromArgb(74,60,140);
  43.       gv.HeaderStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
  44.       gv.HeaderStyle.Font.Names = new string[] { "Tahoma","Arial","Verdana","Trebuchet","Sans-Serif" };
  45.       gv.HeaderStyle.Font.Size = new System.Web.UI.WebControls.FontUnit("9pt");
  46.       gv.HeaderStyle.Font.Bold = true;
  47.       gv.HeaderStyle.Wrap = true;
  48.       gv.AlternatingRowStyle.BackColor = System.Drawing.Color.FromArgb(247,247,247);
  49.       gv.FooterStyle.BackColor = System.Drawing.Color.FromArgb(181,199,222);
  50.       gv.FooterStyle.ForeColor = System.Drawing.Color.FromArgb(74,60,140);
  51.       gv.FooterStyle.Font.Names = new string[] { "Tahoma","Arial","Verdana","Trebuchet","Sans-Serif" };
  52.       gv.FooterStyle.Font.Size = new System.Web.UI.WebControls.FontUnit("8pt");
  53.       gv.DataBound += new System.EventHandler(gvInvoices_DataBound);
  54.       gv.PageIndexChanging += new System.Web.UI.WebControls.GridViewPageEventHandler(gvInvoices_PageIndexChanging);
  55.       gv.Sorting += new System.Web.UI.WebControls.GridViewSortEventHandler(gvInvoices_Sorting);
  56.       gv.DataSource = tblTmp1;
  57.       gv.DataBind();
  58.  
May 18 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

7 posts views Thread by Brett | last post: by
4 posts views Thread by rushikesh.joshi | last post: by
3 posts views Thread by SAL | last post: by
1 post views Thread by Nathan Sokalski | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.