4

In a DataGridView the user can select multiple rows that aren't adjacent to each other by holding control and selecting different rows. My question has two parts.

Firstly, is holding control by default the only way the user can select multiple rows that aren't adjacent to each other like this? Secondly, how would one disable this behavior?

Ryan Peschel
  • 11,087
  • 19
  • 74
  • 136

3 Answers3

3

Firstly, is holding control by default the only way the user can select multiple rows that aren't adjacent to each other like this?

No, you can have it by your own way by implemeting Events. These links may help you achieve what you want: this and this.

Secondly, how would one disable this behavior?

Disable the ctrl+click so that the user cannot select multiple cells, rows or columns that are apart from each other. You can do this by overriding the OnMouseDown event. Obviously for this you will have to go for your own (datagridview inherited) control. In that override the OnMouseDown event by..

 protected override void OnMouseDown(MouseEventArgs e)
    {
        if ((Control.ModifierKeys & Keys.Control) == Keys.Control)
        {
        }
        else
        {
            base.OnMouseDown(e);
        }
    }     

Hope it helps.

Saurabh R S
  • 3,037
  • 1
  • 34
  • 44
2

Use multiSellect property like this

multiSelect=false

Marko
  • 20,385
  • 13
  • 48
  • 64
0

Without having to derive a custom datagrid you might be able to keep track of the selected row indexes and unselect a row if it is invalid in the RowStateChanged event. This assumes fullrow selection.

You would also have to add some validation logic on row deselection to make sure that didn't create a gap in adjacent rows. For example row 1,2,3 are selected then row 2 is deselected so you would need to decide to either disallow this or deselect row 3 or row 1.

public List<int> selectedIndexes = new List<int>();

private void dataGridView1_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
{
     if (e.StateChanged == DataGridViewElementStates.Selected)
     {
          //selected
          if (e.Row.Selected)
          { 
              int newRowIndex = e.Row.Index;

              //if other rows selected make sure adjacent selection exists                   
              if (selectedIndexes.Count() > 0)
              {
                  if (selectedIndexes.Contains(newRowIndex - 1) || selectedIndexes.Contains(newRowIndex + 1))
                  {
                            //allow selection
                        selectedIndexes.Add(newRowIndex);
                  }
                  else
                  {
                        //cancel selection
                        e.Row.Selected = false;
                  }
               }
               else
               {
                   //first selection so allow it
                    selectedIndexes.Add(newRowIndex);
                }
           }
           else if( !e.Row.Selected)
           {
                   //row deselected (need to add logic to remove non adjacent rows to be unselected as well)
                    selectedIndexes.Remove(e.Row.Index);
           }
     }

}

jrob
  • 532
  • 1
  • 6
  • 16