GridViewCheckBoxColumn error in "all checked" cell with exact 2 rows

1 Answer 32 Views
GridView
Julian
Top achievements
Rank 1
Iron
Iron
Iron
Julian asked on 26 Sep 2024, 07:58 AM

Hello

I have a hierarchical RadGridview with a GridViewCheckBoxColumn in the secondary template.

The column has EnableHeaderCheckBox = true so that it shows if all rows are checked and can be used to check them all.

If there are exactly two rows and I manually check the first of them the 'all checked' header checkbox gets checked - although the second row is not checked.

As soon as the second row is checked the error doesnt occur anymore

Julian
Top achievements
Rank 1
Iron
Iron
Iron
commented on 26 Sep 2024, 01:02 PM

Noticed that the error also occurs with more than two rows if all but the last one are checked

1 Answer, 1 is accepted

Sort by
0
Nadya | Tech Support Engineer
Telerik team
answered on 30 Sep 2024, 01:53 PM

Hello, Julian,

Thank you for writing.

I was able to replicate the above-described behavior when using the GridViewCheckBoxColumn. I confirm that the column does not behave correctly only the first time when you check/uncheck rows. This happens for the MasterTemplate and child templates. I have logged this on your behalf in our feedback portal: RadGridView: GridViewCheckBoxColumn incorrectly shows a header check mark when the last row still remains unchecked (telerik.com)

I updated your Telerik Points for bringing this to our attention. 

Currently, the possible solution that I can suggest is to create a custom GridCheckBoxHeaderCellElement and override its SetTwoStateCheckBoxState method in the following way. You can see the achieved result in the attached gif file. 

 public class CustomCheckBoxHeaderCellElement : GridCheckBoxHeaderCellElement
 {
     public CustomCheckBoxHeaderCellElement(GridViewColumn column, GridRowElement row) : base(column, row)
     {
     }

     protected override void SetTwoStateCheckBoxState()
     {
         List<GridViewRowInfo> rows = new List<GridViewRowInfo>();
         MethodInfo mi = typeof(GridCheckBoxHeaderCellElement).GetTypeInfo().GetMethod("GetRowsToIterateOver", BindingFlags.Instance | BindingFlags.NonPublic);
         rows = mi.Invoke(this, null) as List<GridViewRowInfo>;

         bool? foundChecked = null;
         bool? foundUnchecked = null;
         bool? foundIndeterminate = null;

         bool isStateEvaluated = this.EvaluateCheckBoxState(rows, ref foundChecked, ref foundUnchecked, ref foundIndeterminate);
         if (!isStateEvaluated)
         {
             if (foundChecked.HasValue && !foundUnchecked.HasValue && !foundIndeterminate.HasValue)
             {
                 this.SetCheckBoxState(ToggleState.On);
             }
             else
             {
                 this.SetCheckBoxState(ToggleState.Off);
             }
         }
     }
 }

Then, replace the default GridCheckBoxHeaderCellElement with the custom one using the CreateCell event:

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

I hope this helps. If you have any other questions do not hesitate to contact me. 

Regards,
Nadya | Tech Support Engineer
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
GridView
Asked by
Julian
Top achievements
Rank 1
Iron
Iron
Iron
Answers by
Nadya | Tech Support Engineer
Telerik team
Share this question
or