Completed
Last Updated: 07 Dec 2016 14:06 by ADMIN
ADMIN
Dess | Tech Support Engineer, Principal
Created on: 15 Apr 2016 07:45
Category: GridView
Type: Bug Report
0
FIX. RadGridView - NullReferenceException when adding a FilterDescriptor to the child template before populating with data
To reproduce:

private void Form1_Load(object sender, EventArgs e)
{ 
    this.suppliersTableAdapter.Fill(this.nwindDataSet.Suppliers); 
    this.productsTableAdapter.Fill(this.nwindDataSet.Products);

    radGridView1.DataSource = nwindDataSet.Suppliers;
    radGridView1.BestFitColumns(BestFitColumnMode.AllCells);
    GridViewTemplate template = CreateTemplate(this.productsBindingSource);
    template.DataSource = nwindDataSet.Products;
    radGridView1.MasterTemplate.Templates.Add(template);

    GridViewRelation relation = new GridViewRelation(radGridView1.MasterTemplate);
    relation.ChildTemplate = template;
    relation.RelationName = "SuppliersProducts";
    relation.ParentColumnNames.Add("SupplierID");
    relation.ChildColumnNames.Add("SupplierID");
    radGridView1.Relations.Add(relation);
}

private GridViewTemplate CreateTemplate(BindingSource source)
{
    GridViewTemplate template = new GridViewTemplate();
    template.Caption = "TaskTemplate";
    template.AllowAddNewRow = false;
    template.EnableFiltering = true;
    FilterDescriptor filter = new FilterDescriptor();
    filter.PropertyName = "ReorderLevel";
    filter.Operator = FilterOperator.IsNotEqualTo;
    filter.Value = 25;
    template.FilterDescriptors.Add(filter);
    template.ShowFilteringRow = false;   
    template.SortDescriptors.Expression = "ProductName ASC, UnitPrice ASC";
    template.DataSource = source;
    template.BestFitColumns(BestFitColumnMode.AllCells);

    return template;
}

Workaround: set the filter after the hierarchy setup:
private void Form1_Load(object sender, EventArgs e)
{ 
    this.suppliersTableAdapter.Fill(this.nwindDataSet.Suppliers); 
    this.productsTableAdapter.Fill(this.nwindDataSet.Products);

    radGridView1.DataSource = nwindDataSet.Suppliers;
    radGridView1.BestFitColumns(BestFitColumnMode.AllCells);
    GridViewTemplate template = CreateTemplate(this.productsBindingSource);
    template.DataSource = nwindDataSet.Products;
    radGridView1.MasterTemplate.Templates.Add(template);

    GridViewRelation relation = new GridViewRelation(radGridView1.MasterTemplate);
    relation.ChildTemplate = template;
    relation.RelationName = "SuppliersProducts";
    relation.ParentColumnNames.Add("SupplierID");
    relation.ChildColumnNames.Add("SupplierID");
    radGridView1.Relations.Add(relation);
    
    FilterDescriptor filter = new FilterDescriptor();
    filter.PropertyName = "ReorderLevel";
    filter.Operator = FilterOperator.IsNotEqualTo;
    filter.Value = 25; 
    template.FilterDescriptors.Add(filter);
}

private GridViewTemplate CreateTemplate(BindingSource source)
{
    GridViewTemplate template = new GridViewTemplate();
    template.Caption = "TaskTemplate";
    template.AllowAddNewRow = false;
    template.EnableFiltering = true;
  
    template.ShowFilteringRow = false;   
    template.SortDescriptors.Expression = "ProductName ASC, UnitPrice ASC";
    template.DataSource = source;
    template.BestFitColumns(BestFitColumnMode.AllCells);
    
   
    return template;
}

0 comments