Completed
Last Updated: 12 Oct 2015 16:07 by ADMIN
ADMIN
Dess | Tech Support Engineer, Sr.
Created on: 17 Sep 2015 12:22
Category: GridView
Type: Bug Report
0
FIX. RadGridView - NullReferenceException when the filter button for Excel-like filtering is clicked for GridViewDateTimeColumn with null values
To reproduce: use the following code snippet and try to filter the second column:

public Form1()
{
    InitializeComponent();

    this.radGridView1.Columns.Add("Id");
    GridViewDateTimeColumn dateTimeColumn = new GridViewDateTimeColumn();
    dateTimeColumn.FilteringMode = GridViewTimeFilteringMode.Date;
    dateTimeColumn.Name = "NullDateTimeColumn";
    dateTimeColumn.HeaderText = "Order date";
    dateTimeColumn.FieldName = "OrderDate"; 
    dateTimeColumn.FormatString = "{0:dd/MM/yyyy}";
    this.radGridView1.Columns.Add(dateTimeColumn);

    dateTimeColumn = new GridViewDateTimeColumn();
    dateTimeColumn.FilteringMode = GridViewTimeFilteringMode.Date;
    dateTimeColumn.Name = "DateTimeColumn";
    dateTimeColumn.HeaderText = "Not Null Date date";
    dateTimeColumn.FieldName = "NotNullDate";
    dateTimeColumn.FormatString = "{0:dd/MM/yyyy}";
    this.radGridView1.Columns.Add(dateTimeColumn);
    this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
    for (int i = 0; i < 50; i++)
    {
        if (i % 2 == 0)
        {
            this.radGridView1.Rows.Add(new object[] { i, null, DateTime.Today });
        }
        else
        { 
            this.radGridView1.Rows.Add(new object[] { i, DateTime.Now.AddDays(i), DateTime.Today });
        }
    }
    this.radGridView1.EnableFiltering = true;
    this.radGridView1.MasterTemplate.ShowHeaderCellButtons = true;
    this.radGridView1.MasterTemplate.ShowFilteringRow = false;
}

private void radGridView1_FilterPopupRequired(object sender, FilterPopupRequiredEventArgs e)
{
    e.FilterPopup = new RadListFilterPopup(e.Column, true);
}

Workaround: use a specific DateTime value instead of null (e.g. 1/1/1800) and use the CellFormatting event to clear the displayed text. Afterwards, in the RadListFilterPopup it is necessary to hide the text for the null value date nodes:

public Form1()
{
    InitializeComponent();

    this.radGridView1.Columns.Add("Id");
    GridViewDateTimeColumn dateTimeColumn = new GridViewDateTimeColumn();
    dateTimeColumn.FilteringMode = GridViewTimeFilteringMode.Date;
    dateTimeColumn.Name = "NullDateTimeColumn";
    dateTimeColumn.HeaderText = "Order date";
    dateTimeColumn.FieldName = "OrderDate"; 
    dateTimeColumn.FormatString = "{0:dd/MM/yyyy}";
   
    this.radGridView1.Columns.Add(dateTimeColumn);

    dateTimeColumn = new GridViewDateTimeColumn();
    dateTimeColumn.FilteringMode = GridViewTimeFilteringMode.Date;
    dateTimeColumn.Name = "DateTimeColumn";
    dateTimeColumn.HeaderText = "Not Null Date date";
    dateTimeColumn.FieldName = "NotNullDate";
    dateTimeColumn.FormatString = "{0:dd/MM/yyyy}";
    this.radGridView1.Columns.Add(dateTimeColumn);
    this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
    for (int i = 0; i < 50; i++)
    {
        if (i % 2 == 0)
        {
            this.radGridView1.Rows.Add(new object[] { i, new DateTime(1800, 1, 1), DateTime.Today });
        }
        else
        { 
            this.radGridView1.Rows.Add(new object[] { i, DateTime.Now.AddDays(i), DateTime.Today });
        }
    }
    this.radGridView1.EnableFiltering = true;
    this.radGridView1.MasterTemplate.ShowHeaderCellButtons = true;
    this.radGridView1.MasterTemplate.ShowFilteringRow = false;
}

private void radGridView1_FilterPopupRequired(object sender, FilterPopupRequiredEventArgs e)
{
    RadListFilterPopup popup = new RadListFilterPopup(e.Column, true);
    e.FilterPopup = popup;
    popup.PopupOpened += popup_PopupOpened;
}

private void popup_PopupOpened(object sender, EventArgs args)
{
    RadListFilterPopup popup = sender as RadListFilterPopup;
    if (popup != null)
    {
        popup.MenuTreeElement.TreeView.NodeFormatting += TreeView_NodeFormatting;
        popup.MenuTreeElement.TreeView.Focus();
    }
}

private void TreeView_NodeFormatting(object sender, TreeNodeFormattingEventArgs e)
{
    if (e.NodeElement.ContentElement.Text.Contains("1800"))
    {
        e.NodeElement.ContentElement.DrawText = false;
        e.NodeElement.ExpanderElement.Opacity = 0;
        e.NodeElement.ExpanderElement.ShouldHandleMouseInput = false;
        e.NodeElement.ShouldHandleMouseInput = false;
    }
    else
    { 
        e.NodeElement.ContentElement.DrawText = true;
        e.NodeElement.ExpanderElement.Opacity = 1;
        e.NodeElement.ExpanderElement.ShouldHandleMouseInput = true;
        e.NodeElement.ShouldHandleMouseInput = true;
    }
}

private void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
{
    GridViewDateTimeColumn dtCol = e.Column as GridViewDateTimeColumn;
    if (e.Row is GridViewDataRowInfo && dtCol != null && ((DateTime)e.CellElement.Value).Year == 1800)
    {
        e.CellElement.Text = string.Empty;
    }
}
Attached Files:
0 comments