C1TrueDBGrid for Winforms provides various selection methods for selecting Rows using Control Key or Shift Key. For information on available Selection mode, you can refer to this documentation link. However, there is a small user based requirement which is not met with these given selection modes. Customer often asks for the requirement to select a Cell Range using the combination of Mouse and Control Key. Let say customer selects a particular cell (1,1). Now they select another cell at 4,4 using Control Key. So their requirement is to select a Cross-Sectional area from 1,1 to 4,4. If we look at the implementation, the first solution that comes into the mind is to add the required Rows and Columns into the SelectedRows and SelectedCols collection. However, this does not meet our requirement as adding any row or column in SelectedRows and SelectedCols collection selects the entire row and column and not the cross-sectional area of the CellRange. So the workaround is to highlight the CellRange manually rather than adding to the selected rows and column collections. We begin the implementation by setting few required Properties. Along with this, we have to maintain a temporary list of Row collection for which the rows have to be highlighted.


 foreach (C1.Win.C1TrueDBGrid.C1DisplayColumn dc in c1TrueDBGrid1.Splits[0].DisplayColumns)  
   dc.FetchStyle = true;  

 c1TrueDBGrid1.MarqueeStyle = C1.Win.C1TrueDBGrid.MarqueeEnum.NoMarquee;  
 c1TrueDBGrid1.MultiSelect = C1.Win.C1TrueDBGrid.MultiSelectEnum.None;  

 int startRow, endRow;  
 List<int> selRowList;  
 selRowList = new List<int>();  


Following code snippet tracks the Rows collection which would be highlighted.



void c1TrueDBGrid1_MouseClick(object sender, MouseEventArgs e)  
{  
    if (startRow == -1)  
    {  
       selRowList.Clear();  
       startRow = c1TrueDBGrid1.RowContaining(e.Y);  
       selRowList.Add(startRow);  
    }  
    else  
    {  
       if (ModifierKeys == Keys.Control)  
       {  
          selRowList.Clear();  
          endRow = c1TrueDBGrid1.RowContaining(e.Y);  
          int counter = 0;  
          if (endRow > startRow)  
             counter = 1;  
          else if (endRow < startRow)  
             counter = -1;  

          for (int i = startRow; i != endRow; i += counter)  
          {  
             selRowList.Add(i);  
          }  

         selRowList.Add(endRow);  

         startRow = -1;  
         endRow = -1;  

      }  
      else  
      {  
          selRowList.Clear();  
          c1TrueDBGrid1.ClearCellStyle(C1.Win.C1TrueDBGrid.CellStyleFlag.AllCells);  
          startRow = c1TrueDBGrid1.RowContaining(e.Y);  
          selRowList.Add(startRow);  
      }  
  }  

  c1TrueDBGrid1.Refresh();  
}  


Final implementation requires highlighting the cell range using FetchCellStyle event.


void c1TrueDBGrid1_FetchCellStyle(object sender, C1.Win.C1TrueDBGrid.FetchCellStyleEventArgs e)  
{  
   if (e.Col >= 1 && e.Col <= 4)  
   {  
       if (selRowList.Contains(e.Row))  
          e.CellStyle.BackColor = Color.LightGreen;  
   }  
}  

Click on the Image to see the demonstration. Refer to the attached samples for complete implementation. Sample C# Sample VB