Completed
Last Updated: 21 Oct 2015 12:37 by ADMIN
ADMIN
Dess | Tech Support Engineer, Principal
Created on: 06 Jul 2015 07:42
Category: GridView
Type: Bug Report
0
FIX. RadGridView - when the GridViewCheckBoxColumn.ThreeState and EnableHeaderCheckBox properties are set to true, header check-box does not display three states
Workaround:

public Form1()
{
    InitializeComponent();
    this.radGridView1.CreateCell += radGridView1_CreateCell;
    GridViewCheckBoxColumn checkBoxColumn = new GridViewCheckBoxColumn("CheckBoxColumn");
    checkBoxColumn.EnableHeaderCheckBox = false;
    checkBoxColumn.ThreeState = true;
    checkBoxColumn.EditMode = EditMode.OnValueChange;
   
    radGridView1.MasterTemplate.Columns.Add(checkBoxColumn);
    radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
    for (int i = 0; i < 20; i++)
    {
        this.radGridView1.Rows.Add(false);
    }
}

private void radGridView1_CreateCell(object sender, GridViewCreateCellEventArgs e)
{
    if (e.CellType == typeof(GridCheckBoxHeaderCellElement))
    {
        CustomGridCheckBoxHeaderCellElement headerCell = new CustomGridCheckBoxHeaderCellElement(e.Column, e.Row);
        e.CellElement = headerCell;
    }
}

public class CustomGridCheckBoxHeaderCellElement : GridHeaderCellElement
{
    public CustomGridCheckBoxHeaderCellElement(GridViewColumn column, GridRowElement row) : base(column, row)
    {
    }

    protected override Type ThemeEffectiveType     
    { 
        get    
        { 
            return typeof(GridHeaderCellElement);     
        }
    }

    RadCheckBoxElement cb = new RadCheckBoxElement();

    protected override void CreateChildElements()
    {
        base.CreateChildElements();
        this.Children.Add(cb);
        cb.IsThreeState = true;
        cb.ToggleState = Telerik.WinControls.Enumerations.ToggleState.Off;
        cb.ToggleStateChanged += CheckBox_ToggleStateChanged;
    }

    bool boolValue = false;

    public override void SetContent()
    {
        base.SetContent();

        if (!flag)
        {
            bool atLeastOneTrue = false;
            bool atLeastOneFalse = false;
            foreach (GridViewRowInfo r in this.GridViewElement.Template.Rows)
            {
                if (r.Cells[0].Value != null)
                {
                    if (atLeastOneTrue && atLeastOneFalse)
                    {
                        break;
                    }

                    boolValue = (bool)r.Cells[0].Value;
                    if (boolValue)
                    {
                        atLeastOneTrue = true;
                    }
                    else
                    {
                        atLeastOneFalse = true;
                    }
                }
                else
                {
                    atLeastOneFalse = true;
                }
            }

            if (atLeastOneFalse && atLeastOneTrue)
            {
                cb.ToggleStateChanged -= CheckBox_ToggleStateChanged;
                cb.ToggleState = Telerik.WinControls.Enumerations.ToggleState.Indeterminate;
                cb.ToggleStateChanged += CheckBox_ToggleStateChanged;
            }
            else if (atLeastOneFalse == true)
            {
                cb.ToggleStateChanged -= CheckBox_ToggleStateChanged;
                cb.ToggleState = Telerik.WinControls.Enumerations.ToggleState.Off;
                cb.ToggleStateChanged += CheckBox_ToggleStateChanged;
            }
            else if (atLeastOneTrue == true)
            {
                cb.ToggleStateChanged -= CheckBox_ToggleStateChanged;
                cb.ToggleState = Telerik.WinControls.Enumerations.ToggleState.On;
                cb.ToggleStateChanged += CheckBox_ToggleStateChanged;
            }
        }
    }

    bool flag = false;

    private void CheckBox_ToggleStateChanged(object sender, StateChangedEventArgs args)
    {
        cb.ToggleStateChanged -= CheckBox_ToggleStateChanged;
        flag = true;
        if (args.ToggleState == Telerik.WinControls.Enumerations.ToggleState.Indeterminate)
        {
            cb.ToggleState = Telerik.WinControls.Enumerations.ToggleState.Off;                   
        }
        ToggleStateForChildRows(args.ToggleState, this.GridControl);
        flag = false;
        cb.ToggleStateChanged += CheckBox_ToggleStateChanged;
    }

    private void ToggleStateForChildRows(Telerik.WinControls.Enumerations.ToggleState toggleState, RadGridView radGridView)
    {
        foreach (GridViewRowInfo r in radGridView.Rows)
        {
            r.Cells[0].Value = toggleState == Telerik.WinControls.Enumerations.ToggleState.On;
        }
    }
}
0 comments