This is a migrated thread and some comments may be shown as answers.

Programmatically created grid throws error if AllowFilteringByColumn = true

3 Answers 109 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kyle Smith
Top achievements
Rank 1
Kyle Smith asked on 03 Jul 2013, 01:25 PM
I am using the latest Q2 release of 2013 .NET version 4.5.

I have a method that creates a grid programmatically. This method is called on Page_Init wherever it is used. Grid is created and works just fine as long as I do not add "AllowFilteringByColumn = true". Here is the body of the method:
RadGrid grid = new RadGrid();
grid.ID = "CustomViewGrid";
grid.AllowFilteringByColumn = true;
grid.AutoGenerateColumns = false;
grid.AllowPaging = true;
grid.AllowSorting = true;
grid.PageSize = view.PageSize;
grid.ClientSettings.Scrolling.AllowScroll = true;
grid.ClientSettings.Scrolling.SaveScrollPosition = true;
grid.ClientSettings.Scrolling.ScrollHeight = Unit.Pixel( 500 );
grid.ClientSettings.Scrolling.UseStaticHeaders = true;
grid.ClientSettings.Selecting.AllowRowSelect = true;
grid.ClientSettings.Selecting.UseClientSelectColumnOnly = true;
grid.AllowMultiRowSelection = true;
grid.GroupingSettings.CaseSensitive = false;
grid.PagerStyle.Mode = GridPagerMode.NextPrevNumericAndAdvanced;
grid.MasterTableView.AutoGenerateColumns = false;
grid.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.None;
grid.MasterTableView.DataKeyNames = new string[] { view.TableKeyName };
return grid;


I have methods to create the columns, which are added to the master table view column collection:
GridBoundColumn column = new GridBoundColumn();
column.DataField = dataField;
column.UniqueName = dataField;
column.AllowFiltering = true;
column.CurrentFilterFunction = GridKnownFunction.Contains;
column.FilterControlWidth = Unit.Pixel(100);
column.ItemStyle.Width = Unit.Pixel(120);
column.HeaderStyle.Width = Unit.Pixel(120);
column.AutoPostBackOnFilter = true;
column.ShowFilterIcon = false;
return column;

The datasource on the grid is set as follows:
EntityDataSource dataSource = new EntityDataSource();
dataSource.ID = "CustomViewGridDataSource";
dataSource.EntitySetName = view.TableName;
dataSource.ContextCreating += dataSource_ContextCreating; // set the object context here since I am using code-first EF5.0
dataSource.Where = view.Filter;
grid.DataSource = dataSource;

Finally, the grid is added to an ASP Panel placeholder (happens in Page_Init).

I get the following error when having AllowFilteringByColumn = true:

Server Error in '/' Application.

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 

[NullReferenceException: Object reference not set to an instance of an object.]
   Telerik.Web.UI.GridTableView.CreateDataSourceSelectArguments() +1330
   System.Web.UI.WebControls.DataBoundControl.PerformSelect() +66
   Telerik.Web.UI.GridTableView.PerformSelect() +38
   System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +30
   Telerik.Web.UI.GridTableView.DataBind() +391
   Telerik.Web.UI.RadGrid.DataBind() +191
   Telerik.Web.UI.RadGrid.AutoDataBind(GridRebindReason rebindReason) +4252
   Telerik.Web.UI.RadGrid.OnLoad(EventArgs e) +201
   System.Web.UI.Control.LoadRecursive() +54
   System.Web.UI.Control.LoadRecursive() +145
   System.Web.UI.Control.LoadRecursive() +145
   System.Web.UI.Control.LoadRecursive() +145
   System.Web.UI.Control.LoadRecursive() +145
   System.Web.UI.Control.LoadRecursive() +145
   System.Web.UI.Control.LoadRecursive() +145
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772


Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18045

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 04 Jul 2013, 04:51 AM
Hi Kyle,

I tried to replicate your issue but no avail. Please try the below code snippet.

C#:
protected void Page_Init(object sender, EventArgs e)
  {
      DefineGridStructure();
  }
 
  private void DefineGridStructure()
  {
      RadGrid grid = new RadGrid();
      grid.ID = "CustomViewGrid";
      grid.AllowFilteringByColumn = true;
      grid.AutoGenerateColumns = false;
      grid.AllowPaging = true;
      grid.AllowSorting = true;
      grid.PageSize =10;
      grid.ClientSettings.Scrolling.AllowScroll = true;     
      grid.ClientSettings.Scrolling.ScrollHeight = Unit.Pixel(500);      
      grid.GroupingSettings.CaseSensitive = false;
      grid.PagerStyle.Mode = GridPagerMode.NextPrevNumericAndAdvanced;  
      grid.MasterTableView.DataKeyNames = new string[] { "OrderID" };      
      grid.DataSourceID = "SqlDataSource2";
 
      GridBoundColumn column = new GridBoundColumn();
      column.DataField = "OrderID";
      column.UniqueName = "OrderID";
      grid.MasterTableView.Columns.Add(column);
      column.AllowFiltering = true;
      column.CurrentFilterFunction = GridKnownFunction.EqualTo;
      column.FilterControlWidth = Unit.Pixel(100);
      column.ItemStyle.Width = Unit.Pixel(120);
      column.HeaderStyle.Width = Unit.Pixel(120);
      column.AutoPostBackOnFilter = true;
      column.ShowFilterIcon = false;  
      PlaceHolder1.Controls.Add(grid);
  }

Thanks,
Princy

0
Kyle Smith
Top achievements
Rank 1
answered on 04 Jul 2013, 03:24 PM
It appears to only happen when using an entity data source like i am doing. See below for a more complete code example:

protected void Page_Init(object sender, EventArgs e)
{
    DefineGridStructure();
}
 
protected void Page_Load(object sender, EventArgs e)
{
 
}
 
private void DefineGridStructure()
{
    RadGrid grid = new RadGrid();
    grid.ID = "CustomViewGrid";
    grid.AllowFilteringByColumn = true;
    grid.AutoGenerateColumns = false;
    grid.AllowPaging = true;
    grid.AllowSorting = true;
    grid.PageSize = 15;
    grid.ClientSettings.Scrolling.AllowScroll = true;
    grid.ClientSettings.Scrolling.SaveScrollPosition = true;
    grid.ClientSettings.Scrolling.ScrollHeight = Unit.Pixel(500);
    grid.ClientSettings.Scrolling.UseStaticHeaders = true;
    grid.ClientSettings.Selecting.AllowRowSelect = true;
    grid.ClientSettings.Selecting.UseClientSelectColumnOnly = true;
    grid.AllowMultiRowSelection = true;
    grid.GroupingSettings.CaseSensitive = false;
    grid.PagerStyle.Mode = GridPagerMode.NextPrevNumericAndAdvanced;
    grid.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.None;
    grid.MasterTableView.DataKeyNames = new string[] { "MyEntityId" };
 
    EntityDataSource dataSource = new EntityDataSource();
    dataSource.ID = "CustomViewGridDataSource";
    dataSource.EntitySetName = "MyEntities";
    dataSource.ContextCreating += dataSource_ContextCreating; // set the object context here since I am using code-first EF5.0
 
    grid.DataSource = dataSource;
 
    GridBoundColumn column = new GridBoundColumn();
    column.DataField = "MyProperty";
    column.UniqueName = "MyProperty";
    column.AllowFiltering = true;
    column.CurrentFilterFunction = GridKnownFunction.Contains;
    column.FilterControlWidth = Unit.Pixel(100);
    column.ItemStyle.Width = Unit.Pixel(120);
    column.HeaderStyle.Width = Unit.Pixel(120);
    column.AutoPostBackOnFilter = true;
    column.ShowFilterIcon = false;
 
    grid.MasterTableView.Columns.Add(column);
 
    PlaceHolder1.Controls.Add(grid);
}
 
private void dataSource_ContextCreating(object sender, EntityDataSourceContextCreatingEventArgs e)
{
    var db = new DataContext();
    e.Context = (db as IObjectContextAdapter).ObjectContext;
}


My data context and single entity:

public class DataContext : DbContext
{
    public DbSet<MyEntity> MyEntities { get; set; }
}
 
 
public class MyEntity
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int MyEntityId { get; set; }
 
    public string MyProperty { get; set; }
}
0
Kyle Smith
Top achievements
Rank 1
answered on 04 Jul 2013, 03:34 PM
I appear to have resolved it by doing the following:

EntityDataSource dataSource = new EntityDataSource();
dataSource.ID = "CustomViewGridDataSource";
dataSource.EntitySetName = "MyEntities";
dataSource.ContextCreating += dataSource_ContextCreating; // set the object context here since I am using code-first EF5.0
PlaceHolder1.Controls.Add(dataSource);
 
grid.DataSourceID = "CustomViewGridDataSource";

I added the datasource to the page and set the DataSourceID rather than setting the data source directly.
Tags
Grid
Asked by
Kyle Smith
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Kyle Smith
Top achievements
Rank 1
Share this question
or