C1TrueDBGrid supports multiple selections of rows through record selector by setting MultiSelect property to MultiSelectEnum.Simple|MultiSelectEnum.Extended. Some customers want this functionality to be implemented by clicking on the row rather than using the record selector. We do have a Multiple Selection sample which shows selection of rows using mouse click. However, it does not take into account Ctrl\Shift keys. Here, in this blog we will discuss how to select multiple rows using mouse click and Ctrl\Shift keys. Here, we need to handle selection of rows manually, therefore, we would need to cancel the automatic selection of rows by setting the Cancel property of CancelEventArgs to true in SelChange event.

 void c1TrueDBGrid1_SelChange(object sender, C1.Win.C1TrueDBGrid.CancelEventArgs e)  
{  
  e.Cancel = true;  
}  

Now, we just need to play around with SelectedRows collection in the RowColChange event. Every time the focus moves to a new cell, we will update the SelectedRows collection considering the Ctrl\Shift key pressed. In case the Ctrl key is pressed, we will try to add or remove the clicked row depending on whether it is already present in the SelectedRows collection. For Shift key being pressed, we will select rows between the first row in SelectedRows collection and the current row (being clicked).


//Multiple Selection code will be handled here based on Ctrl or Shift key  
void c1TrueDBGrid1_RowColChange(object sender, C1.Win.C1TrueDBGrid.RowColChangeEventArgs e)  
{  
   //Control key is pressed : Add or delete the row from selection depending on whether the row is in SelectedRows Collection  
 if (((Control.ModifierKeys & Keys.Control) != 0) && c1TrueDBGrid1.MultiSelect != C1.Win.C1TrueDBGrid.MultiSelectEnum.None)  
 {  
   if (c1TrueDBGrid1.SelectedRows.IndexOf(c1TrueDBGrid1.Row) > -1)  
    {  
      c1TrueDBGrid1.SelectedRows.RemoveAt(c1TrueDBGrid1.SelectedRows.IndexOf(c1TrueDBGrid1.Row));  
    }  
  else  
   {  
     c1TrueDBGrid1.SelectedRows.Add(c1TrueDBGrid1.Row);  
   }  
}  
else //Shift key is pressed : selects all rows between the first selected row in selected row collection and the currently clicked row:  
 {  
  if (((Control.ModifierKeys & Keys.Shift) != 0) && c1TrueDBGrid1.MultiSelect != C1.Win.C1TrueDBGrid.MultiSelectEnum.None)  
   {  
     int SelRowStartindex = c1TrueDBGrid1.SelectedRows[0];  
     c1TrueDBGrid1.SelectedRows.Clear();  
     c1TrueDBGrid1.SelectedRows.Add(SelRowStartindex);  
     if (c1TrueDBGrid1.SelectedRows.Count > 0)  
      {  
        if (c1TrueDBGrid1.SelectedRows[0] > c1TrueDBGrid1.Row)  
          {  
           for (row = c1TrueDBGrid1.SelectedRows[0]; row >= c1TrueDBGrid1.Row; row += -1)  
            {  
               if (c1TrueDBGrid1.SelectedRows.IndexOf(row) > -1)  
                {  
                  cTrueDBGrid1.SelectedRows.RemoveAt(c1TrueDBGrid1.SelectedRows.IndexOf(row));  
                }  
              c1TrueDBGrid1.SelectedRows.Add(row);  
             }  
          }  
        else  
          {  
          for (row = c1TrueDBGrid1.SelectedRows[0]; row <= c1TrueDBGrid1.Row; row++)            {             if (c1TrueDBGrid1.SelectedRows.IndexOf(row) > -1)  
              {  
                c1TrueDBGrid1.SelectedRows.RemoveAt(c1TrueDBGrid1.SelectedRows.IndexOf(row));  
              }  
             c1TrueDBGrid1.SelectedRows.Add(row);  
           }  
         }  
      }  
   }  
   else  
   {  
      c1TrueDBGrid1.SelectedRows.Clear();  
      c1TrueDBGrid1.SelectedRows.Add(c1TrueDBGrid1.Row);  
   }  
 }  
}  

MultiSelection Refer to the attached sample for complete implementation. Multiple Selection C# Multiple Selection VB