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
16 Answers, 1 is accepted
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
Did this help? If so please remember to mark as answer so others can find the solution too.
Thanks
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
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
Many Thanks
Chris
Richard
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
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
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
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
getting null reference exception in if (row != this.RowInfo && row.Cells[this.ColumnIndex].Value == null || !(bool)row.Cells[this.ColumnIndex].Value)
when select a check box
Hello, Venkat,
Could you please specify what is the Telerik UI for WinForms version that you are currently using? Are you using the EnableHeaderCheckBox property of the column or the custom solution from the previously referred KB article?Could you please specify the exact steps how to reproduce the problem you are facing? Once we replicate the issue locally, we would be able to make an adequate analysis of the precise case and think about a suitable solution. Thank you in advance for your cooperation.
I am looking forward to your reply.
Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik
Hi Dess,
I'm using GridColumncheckbox in our project in 3 levels (Header/master,Parent,child).
I should satisfy following conditions:
1.when header check-box clicked all grid should get selected(both child and parent) and hen unchecked is should get unchecked.
2.when individual parent row get selects the child rows should selected.
3.when all child rows of the parent selected that particular parent should get select.
My Issue: it is with 1st case when I check master checkbox parent rows are getting selected but also child rows are getting selected only when the parent row is collapsed.
Can you help with this issue?
Note:I'm not using customheader checbox.And i'm handling HeaderCellToggleStateChanged event also.
Thanks,
Akarsh
Hello, Akarsh,
By default, the header checkbox is expected to toggle the rows belonging to the respective hierarchy level. The rest of the rows which are a part of other hierarchy templates have to be managed programmatically.
Indeed, handling the HeaderCellToggleStateChanged event is the appropriate place to do it.
Please refer to the following code snippet demonstrating how to update the child levels:
List<GridTableElement> childTableElements = null;
private void radGridView1_ViewCellFormatting(object sender, CellFormattingEventArgs e)
{
if (e.Row.HierarchyLevel > 0 && e.Column.Name == "Discontinued")
{
if (childTableElements == null)
{
childTableElements = new List<GridTableElement>();
}
if (!childTableElements.Contains(e.CellElement.TableElement))
{
childTableElements.Add(e.CellElement.TableElement);
}
}
}
private void radGridView1_HeaderCellToggleStateChanged(object sender, GridViewHeaderCellEventArgs e)
{
if (e.Row.HierarchyLevel == 0)
{
foreach (GridViewDataRowInfo row in this.radGridView1.Rows)
{
foreach (GridViewRowInfo childRow in row.ChildRows)
{
childRow.Cells["Discontinued"].Value = e.State == ToggleState.On ? true : false;
}
}
}
if (childTableElements != null)
{
foreach (GridTableElement t in childTableElements)
{
t.Update(GridUINotifyAction.DataChanged);
}
}
}
I hope this information helps. If you need any further assistance please don't hesitate to contact me.
Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik
Thank You