Completed
Last Updated: 15 Sep 2015 07:44 by ADMIN
ADMIN
Dess | Tech Support Engineer, Principal
Created on: 18 May 2015 14:18
Category: GridView
Type: Bug Report
1
FIX. RadGridView - NullReferenceException when calling the SelectAll method in the HeaderCellToggleStateChanged event handler
To reproduce:

public Form1()
{
    InitializeComponent();

    GridViewCheckBoxColumn checkBoxColumn = new GridViewCheckBoxColumn("Select");
    checkBoxColumn.EnableHeaderCheckBox = true;
    radGridView1.MasterTemplate.Columns.Insert(0, checkBoxColumn);

    for (int i = 0; i < 10; i++)
    {
        this.radGridView1.Rows.Add(false);
    }
    this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;

    this.radGridView1.MultiSelect = true;
    this.radGridView1.HeaderCellToggleStateChanged += radGridView1_HeaderCellToggleStateChanged;
}

private void radGridView1_HeaderCellToggleStateChanged(object sender, GridViewHeaderCellEventArgs e)
{
    if (e.State.ToString() == "On")
        this.radGridView1.SelectAll();
    else
        this.radGridView1.ClearSelection();
}


Workaround 1:
private void radGridView1_HeaderCellToggleStateChanged(object sender, GridViewHeaderCellEventArgs e)
{
    bool selected = e.State.ToString() == "On";

    foreach (GridViewRowInfo r in this.radGridView1.Rows)
    {
        r.IsSelected = selected;
    }
}

Workaround 2:

private void radGridView1_MouseUp(object sender, MouseEventArgs e)
{
    RadCheckBoxElement el = this.radGridView1.ElementTree.GetElementAtPoint(e.Location) as RadCheckBoxElement;
    if (el!=null)
    {
        GridCheckBoxHeaderCellElement headerCell = el.Parent as GridCheckBoxHeaderCellElement;
        bool selected = el.ToggleState == Telerik.WinControls.Enumerations.ToggleState.On;
        if (headerCell!=null)
        {
            if (selected)
            {
                this.radGridView1.SelectAll();
            }
            else
            {
                this.radGridView1.ClearSelection();
            }
        }
    }
}
0 comments