Issue with Select All column checkbox in Radgrid

11 posts, 1 answers
  1. Julian Samuel
    Julian Samuel avatar
    1 posts
    Member since:
    Mar 2010

    Posted 04 Feb 2011 Link to this post

    We are working with Telerik radgrid .Net Win form controls. Basically ours is a data driven application where a scenario occurs as follows

     The grid  has  select all check box column in the radgridview in which the user able to “select” and “Unselect” thorough the header checkbox but when sorting is applied on the checkbox header cell ,the "Select All"  feature works in a odd way (not all rows are getting selected /unselected). Is there any workaround for this?

    Regards
    Julian
  2. Answer
    Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 04 Feb 2011 Link to this post

    Hello Julian,

    There is a good KB Article here on adding a checkbox to a RadGridView header and using it as a check all / uncheck all.

    Hope that helps
    Richard
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 07 Feb 2011 Link to this post

    Hello,

    Did this help? If so please remember to mark as answer so others can find the solution too.
    Thanks
    Richard
  5. Chris
    Chris avatar
    58 posts
    Member since:
    Jun 2012

    Posted 21 Mar 2011 Link to this post

    Hi Richard,
    thanks for posting the link. The select / unselect all works fine, but now if I try and check / uncheck a single item I get a null Reference exception at this block of code in CheckBoxHeaderCell.cs. The offending line is 'this.GridViewElement.EditorManager.EndEdit();'. Here is the stack Trace:

       at Telerik.WinControls.UI.GridCellElement.get_GridViewElement()
       at EC_School_Reports_Manager.CheckBoxHeaderCell.GridControl_ValueChanged(Object sender, EventArgs e) in D:\code\EC School Reports Manager\EC School Reports Manager\CheckBoxHeaderCell.cs:line 114
       at System.EventHandler.Invoke(Object sender, EventArgs e)
       at Telerik.WinControls.UI.RadGridView.OnValueChanged(Object sender, EventArgs e)
       at Telerik.WinControls.UI.EventDispatcher.RaiseEvent[T](Object eventKey, Object sender, T args)
       at Telerik.WinControls.UI.BaseGridEditor.OnValueChanged()
       at Telerik.WinControls.UI.RadCheckBoxEditor.SetValue(Object value)
       at Telerik.WinControls.UI.RadCheckBoxEditor.set_Value(Object value)
       at Telerik.WinControls.UI.RadCheckBoxEditor.ToggleState()
       at Telerik.WinControls.UI.RadCheckBoxEditorElement.OnMouseUp(MouseEventArgs e)
       at Telerik.WinControls.RadElement.OnCLREventsRise(RoutedEventArgs args)
       at Telerik.WinControls.RadElement.OnBubbleEvent(RadElement sender, RoutedEventArgs args)
       at Telerik.WinControls.RadItem.OnBubbleEvent(RadElement sender, RoutedEventArgs args)
       at Telerik.WinControls.RadElement.RaiseBubbleEvent(RadElement sender, RoutedEventArgs args)
       at Telerik.WinControls.RadItem.RaiseBubbleEvent(RadElement sender, RoutedEventArgs args)
       at Telerik.WinControls.RadElement.RaiseBubbleEvent(RadElement sender, RoutedEventArgs args)
       at Telerik.WinControls.RadItem.RaiseBubbleEvent(RadElement sender, RoutedEventArgs args)
       at Telerik.WinControls.RadElement.RaiseRoutedEvent(RadElement sender, RoutedEventArgs args)
       at Telerik.WinControls.RadElement.DoMouseUp(MouseEventArgs e)
       at Telerik.WinControls.ComponentInputBehavior.OnMouseUp(MouseEventArgs e)
       at Telerik.WinControls.RadControl.OnMouseUp(MouseEventArgs e)
       at Telerik.WinControls.UI.RadGridView.OnMouseUp(MouseEventArgs e)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       at Telerik.WinControls.RadControl.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.RunDialog(Form form)
       at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
       at EC_School_Reports_Manager.Main.ChoosePupils_Click(Object sender, EventArgs e) in D:\code\EC School Reports Manager\EC School Reports Manager\Main.cs:line 68
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at Telerik.WinControls.RadControl.OnClick(EventArgs e)
       at Telerik.WinControls.UI.RadButton.ButtonElement_Click(Object sender, EventArgs e)
       at Telerik.WinControls.RadItem.OnClick(EventArgs e)
       at Telerik.WinControls.UI.RadButtonItem.OnClick(EventArgs e)
       at Telerik.WinControls.RadItem.DoClick(EventArgs e)
       at Telerik.WinControls.RadItem.RaiseBubbleEvent(RadElement sender, RoutedEventArgs args)
       at Telerik.WinControls.RadElement.RaiseRoutedEvent(RadElement sender, RoutedEventArgs args)
       at Telerik.WinControls.RadItem.RaiseBubbleEvent(RadElement sender, RoutedEventArgs args)
       at Telerik.WinControls.RadElement.RaiseRoutedEvent(RadElement sender, RoutedEventArgs args)
       at Telerik.WinControls.RadElement.DoMouseUp(MouseEventArgs e)
       at Telerik.WinControls.ComponentInputBehavior.OnMouseUp(MouseEventArgs e)
       at Telerik.WinControls.RadControl.OnMouseUp(MouseEventArgs e)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       at Telerik.WinControls.RadControl.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at EC_School_Reports_Manager.Program.Main() in D:\code\EC School Reports Manager\EC School Reports Manager\Program.cs:line 18
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()

    void GridControl_ValueChanged(object sender, EventArgs e)
           {
               RadCheckBoxEditor editor = sender as RadCheckBoxEditor;
               if (editor != null)
               {
                   this.GridViewElement.EditorManager.EndEdit();
                   if ((ToggleState)editor.Value == ToggleState.Off)
                   {
                       SetCheckBoxState(ToggleState.Off);
                   }
                   else if ((ToggleState)editor.Value == ToggleState.On)
                   {
                       bool found = false;
                       foreach (GridViewRowInfo row in this.ViewInfo.Rows)
                       {
                           if (row != this.RowInfo && row.Cells[this.ColumnIndex].Value == null || !(bool)row.Cells[this.ColumnIndex].Value)
                           {
                               found = true;
                               break;
                           }
                       }
                       if (!found)
                       {
                           SetCheckBoxState(ToggleState.On);
                       }
                   }
               }
           }


    Thanks

    Chris
  6. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 21 Mar 2011 Link to this post

    Hi Chris,

    As far as I can tell, you can now remove the entire GridControl_ValueChanged event handler from the demo code and it should work fine.
    Hope that helps, but let me know if you need more information
    Richard
  7. Chris
    Chris avatar
    58 posts
    Member since:
    Jun 2012

    Posted 21 Mar 2011 Link to this post

    Richard, works like a charm. Might be worth asking for the sample to be updated as that also has this bug.

    Many Thanks

    Chris
  8. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 21 Mar 2011 Link to this post

    Thanks for letting me know Chris. I'll report the issue in the KB Article.
    Richard
  9. Martin Vasilev
    Admin
    Martin Vasilev avatar
    1061 posts

    Posted 24 Mar 2011 Link to this post

    Hello guys,

    Thank you for bringing this issue to our attention. The KB article will be updated to work with latest Q1 2011 release. However, I would like just to add that actually ValueChanged event should not be removed, because it implements the process of unchecking the header cell, if one of the checked rows is manually unchecked. Rather than removing the event, I would suggest that you change the way this event is initialized. Just use the Attached and Detached methods instead of OnLoaded:
    public class CheckBoxHeaderCell : GridHeaderCellElement
     {
         //......
         public override void Attach(GridViewColumn data, object context)
         {
             base.Attach(data, context);
             this.GridControl.ValueChanged += new EventHandler(GridControl_ValueChanged);
         }
         public override void Detach()
         {
             base.Detach();
             this.GridControl.ValueChanged -= GridControl_ValueChanged;
         }
         //......
     }

    I am also attaching the modified projects to this message for your convenience. Let me know if you have any additional questions.

    Greetings,
    Martin Vasilev
    the Telerik team
  10. Chris
    Chris avatar
    58 posts
    Member since:
    Jun 2012

    Posted 24 Mar 2011 Link to this post

    Hi Martin,
       the CS sample works perfectly (haven't used the VB one), many thnkas for fixing this. As always Telerik support is 1st class
    Thanks

    Chris 
  11. swarupa
    swarupa avatar
    11 posts
    Member since:
    Aug 2016

    Posted 12 Oct in reply to Martin Vasilev Link to this post

    Hi,

    I have a three level gridview. Each level has a checkbox column with a header check box in the header row and checkboxes in datarows.

    I have used the following code from Teleirk for checking or unchecking the records in the three levels  gridview.

    While trying to expand the third level gridview, it is throwing an exception.

    If I exclude the GridControl_ValueChanged, Attach and Detach events the code is working fine, but I am unable to check or uncheck the header checkbox if any of the checkboxes in the datarows are checked or unchecked. Any help is appreciated.

     

    private void DgSearchBdPortsResult_CreateCell(object sender, GridViewCreateCellEventArgs e)
    {
    if (e.Row is GridTableHeaderRowElement && e.Column.HeaderText == "Select All")
    {
    e.CellElement = new CheckBoxHeaderCell(e.Column, e.Row);
    }
    }
    bool suspendEvent = false;
    private void DgSearchBdPortsResult_CellValueChanged(object sender, GridViewCellEventArgs e)
    {
    if (!suspendEvent && e.Column.Name == "column1")
    {
    List<GridViewHierarchyRowInfo> parents = new List<GridViewHierarchyRowInfo>();
    GridViewHierarchyRowInfo parent = e.Row.Parent as GridViewHierarchyRowInfo;
    while (parent != null)
    {
    parents.Add(parent);
    parent = parent.Parent as GridViewHierarchyRowInfo;
    }


    suspendEvent = true;
    foreach (GridViewHierarchyRowInfo p in parents)
    {
    p.Cells["column1"].Value = this.CheckCellsForLevel(e.Row.ViewInfo);
    }


    GridViewHierarchyRowInfo hierarchyRow = e.Row as GridViewHierarchyRowInfo;
    if (hierarchyRow != null)
    {
    Queue<GridViewHierarchyRowInfo> children = new Queue<GridViewHierarchyRowInfo>();
    children.Enqueue(hierarchyRow);


    while (children.Count > 0)
    {
    GridViewHierarchyRowInfo current = children.Dequeue();
    foreach (GridViewRowInfo row in current.ChildRows)
    {
    row.Cells["column1"].Value = e.Value;
    if (row is GridViewHierarchyRowInfo)
    {
    children.Enqueue((GridViewHierarchyRowInfo)row);
    }
    }
    }
      }              


                    suspendEvent = false;
    }
    }
    private bool CheckCellsForLevel(GridViewInfo template)
    {
    foreach (GridViewRowInfo rowInfo in template.Rows)
    {
    if ((bool)rowInfo.Cells["column1"].Value == false)
    {
    return false;
    }
    }


    return true;
    }
    private void DgSearchBdPortsResult_ValueChanged(object sender, EventArgs e)
    {
    DgSearchBdPortsResult.EndEdit();
    //GridCheckBoxCellElement checkBoxCell = sender as GridCheckBoxCellElement;
    // if (this.DgSearchBdPortsResult.ActiveEditor is RadCheckBoxEditor || this.DgSearchBdPortsResult.Tag is RadCheckBoxElement)
    {

    }
    //else
    {
    }
    }



     public class CheckBoxHeaderCell : GridHeaderCellElement
    {
    #region Fields
    private RadCheckBoxElement checkbox;
    private int suspendNotificationCount = 0;
    private bool suspendProcessingToggleStateChanged;
    #endregion
    #region Initialization
    public CheckBoxHeaderCell(GridViewColumn column, GridRowElement row)
    : base(column, row)
    {
    }
    protected override void InitializeFields()
    {
    base.InitializeFields();
    this.DrawText = false;
    }
    public override void Initialize(GridViewColumn column, GridRowElement row)
    {
    base.Initialize(column, row);
    column.AllowSort = false;
    }
    protected override void DisposeManagedResources()
    {
    this.checkbox.ToggleStateChanged -= new StateChangedEventHandler(checkbox_ToggleStateChanged);
    base.DisposeManagedResources();
    }
    protected override void CreateChildElements()
    {
    base.CreateChildElements();
    this.checkbox = new RadCheckBoxElement();
    this.checkbox.ToggleStateChanged += new StateChangedEventHandler(checkbox_ToggleStateChanged);
    this.Children.Add(checkbox);
    } #endregion
    #region Properties
    protected override Type ThemeEffectiveType
    {
    get
    {
    return typeof(GridHeaderCellElement);
    }
    }
    public override object Value
    {
    get
    {
    return Convert.ToBoolean(this.RowInfo.Tag);
    }
    set
    {
    this.RowInfo.Tag = value;
    }
    } #endregion
    #region Methods
    public void SetCheckBoxState(Telerik.WinControls.Enumerations.ToggleState state)
    {
    suspendProcessingToggleStateChanged = true;
    this.checkbox.ToggleState = state;
    suspendProcessingToggleStateChanged = false;
    }

    protected override void SetContentCore(object value)
    {
    base.SetContentCore(value);
    this.suspendNotificationCount++;
    if (this.RowInfo.Tag != null)
    {
    this.checkbox.ToggleState = (bool)value ? ToggleState.On : ToggleState.Off;
    }
    else
    {
    this.checkbox.ToggleState = ToggleState.Off;
    }
    this.checkbox.TextElement.Text = this.Text;
    this.suspendNotificationCount--;
    }
    public override bool IsCompatible(GridViewColumn data, object context)
    {
    return data.Name == "Select" && context is GridTableHeaderRowElement
    && base.IsCompatible(data, context);
    }
    #endregion
    #region Event Handlers
    private void checkbox_ToggleStateChanged(object sender, StateChangedEventArgs args)
    {
    if (!suspendProcessingToggleStateChanged)
    {
    for (int i = 0; i < this.GridControl.Rows.Count; i++)
    {
    this.GridControl.Rows[i].Cells[this.ColumnIndex].Value = this.checkbox.IsChecked;
    }
    }

    }
    void GridControl_ValueChanged(object sender, EventArgs e)
    {
    RadCheckBoxEditor editor = sender as RadCheckBoxEditor;
    if (editor != null)
    {
    this.GridViewElement.EditorManager.EndEdit();
    if ((ToggleState)editor.Value == ToggleState.Off)
    {
    SetCheckBoxState(ToggleState.Off);
    }
    else if ((ToggleState)editor.Value == ToggleState.On)
    {
    bool found = false;
    foreach (GridViewRowInfo row in this.ViewInfo.Rows)
    {
    if (row != this.RowInfo && row.Cells[this.ColumnIndex].Value == null || !(bool)row.Cells[this.ColumnIndex].Value)
    {
    found = true;
    break;
    }
    }
    if (!found)
    {
    SetCheckBoxState(ToggleState.On);
    }
    }
    }
    }
    public override void Attach(GridViewColumn data, object context)
    {
    base.Attach(data, context);
    this.GridControl.ValueChanged += new EventHandler(GridControl_ValueChanged);
    }
    public override void Detach()
    {
    base.Detach();
    this.GridControl.ValueChanged -= GridControl_ValueChanged;
    } #endregion
    #region Layout
    protected override SizeF ArrangeOverride(SizeF finalSize)
    {
    SizeF size = base.ArrangeOverride(finalSize);
    RectangleF rect = GetClientRectangle(finalSize);
    this.checkbox.Arrange(new RectangleF((finalSize.Width - this.checkbox.DesiredSize.Width) / 2, (rect.Height - 20) / 2, 20, 20));
    return size;
    } #endregion }

     

    Thanks And Regards,

    Bavya

  12. Hristo
    Admin
    Hristo avatar
    714 posts

    Posted 14 Oct Link to this post

    Hello Swarupa,

    Thank you for writing.

    If you are using the latest version of the controls you can simply set the EnableHeaderCheckBox property of your boolean column. The custom solution as demonstrated here is suitable for older versions. The additional snippet for checking parent rows and child templates is working well on my end. 

    It is possible to experience the reported behavior if you mix the custom implementation of the header cells with newer assemblies. If that is the case please use the EnableHeaderCheckBox property. If you keep experiencing the issue please open a support ticket and send us your project.

    I hope this helps. Should you have further questions please do not hesitate to write back.

    Regards,
    Hristo Merdjanov
    Telerik by Progress
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
Back to Top
UI for WinForms is Visual Studio 2017 Ready