C1FlexGrid: Toggle Multiple CheckBox Cells in Sorted Boolean Column

Ideally if you select multiple CheckBox cells and toggle the value of any of the checkbox from that range, checked state for the other selected checkboxes are also toggled to the similar state. This works perfectly for a non sorted column. However, if the CheckBox column is sorted and you perform the similar operation, you will observe that unwanted Checkboxes are toggled. This happens as the rows are repositioned as per the sorting order once the Checkbox state is changed. This blog provides a solution to handle the toggling of checked state for multiple CheckBoxes together when the column in C1FlexGrid for Winforms is sorted. The problem discussed above is generic and could be seen with most of the grid controls. There could be various ways to handle this behavior and may vary for different grids. In this blog, I used C1FlexGrid to guide our customers. Before looking at the code, let me quickly explain the basic logic of the solution. Its pretty simple. You need to handle the BeforeMouseDown event and cancel the default action when a CheckBox is clicked. C1FlexGrid for Winforms provides the HitTestInfo object to determine where mouse was clicked inside its region. Once the default mouse action is cancelled, loop through the selected cells and toggle the CheckBox values directly in the underlying datasource for the row. The following code snippet should get the implementation going in your application.

/\*\*Store the range of cells selected in a global variable - crange\*\*/  
C1.Win.C1FlexGrid.CellRange cRange;  

void c1FlexGrid1_AfterSelChange(object sender, C1.Win.C1FlexGrid.RangeEventArgs e)  
{  
    cRange = e.NewRange;  
}  

void c1FlexGrid1_BeforeMouseDown(object sender, C1.Win.C1FlexGrid.BeforeMouseDownEventArgs e)  
{  
    if (c1FlexGrid1.HitTest().Type == HitTestTypeEnum.Checkbox)  
    {  
       if (cRange.r1 != cRange.r2)  
       {  
          c1FlexGrid1.BeginUpdate();  

          int col, row;  
          col = c1FlexGrid1.HitTest(e.X, e.Y).Column;  
          row = c1FlexGrid1.HitTest(e.X, e.Y).Row;  

          //Cancel the default mouse down action  
          e.Cancel = true;  

          C1.Win.C1FlexGrid.CheckEnum chValue = c1FlexGrid1.GetCellCheck(row, col);  

          List<DataRowView> drList = new List<DataRowView>();  

          for (int i = cRange.r1; i <= cRange.r2; i++)  
              drList.Add(c1FlexGrid1.Rows[i].DataSource as DataRowView);  

          foreach (DataRowView dr in drList)  
          {  
               switch (chValue)  
               {  
                    case C1.Win.C1FlexGrid.CheckEnum.Checked:  
                    case C1.Win.C1FlexGrid.CheckEnum.TSChecked:  
                              dr["Present"] = false;  
                                break;  
                    case C1.Win.C1FlexGrid.CheckEnum.Unchecked:  
                    case C1.Win.C1FlexGrid.CheckEnum.TSUnchecked:  
                              dr["Present"] = true;  
                                break;  
                }  
           }  

           c1FlexGrid1.EndUpdate();  
        }  
     }  
}

Download the samples for complete implementation. Download C# Sample Download VB.Net Sample

GrapeCity

GrapeCity Developer Tools
comments powered by Disqus