Datagrid multiselect

  • Thread starter Thread starter Erald Kulk
  • Start date Start date
E

Erald Kulk

is it possible to turn off the multiselect in a Datagrid? if yes, how?

Erald Kulk
 
Hello Erald,

This can most likely be done, but requires non-trivial efforts. You will
need to inherit from a DataGrid and override its OnMouseDown,
ProcessDialogKey and ProcessKeyPreview methods in order to tap into keyboard
and mouse navigation and disable multi-select operations.
 
thank's for your answer, but while waiting answers I found another
simpler solution:

private void dataGridClients_MouseUp(object sender, MouseEventArgs e)
{
System.Windows.Forms.DataGrid.HitTestInfo myHitTest =
this.dataGridClients.HitTest(e.X,e.Y);
if (myHitTest.Type == DataGrid.HitTestType.Cell)
{
this.dataGridClients.Select(myHitTest.Row);
}
}

Now only one row is selected, but not when you click on
the rowheader, therefore you have to implement the mouseclick likewise
also and mousemove where you locally store the mouse x and y position
 
Erald,

Now click on a row to select it, hold the Shift key and press the Down key.
Two rows will be selected as a result. More than that, I may be overlooking
something, but as far as I remember, the Select method does not de-select
other already selected rows. DataGrid has a protected ResetSelection (or
named something like that) method to reset current selection.
 
Dmitriy,

so you where right. after testing more I was able to select 2 rows when
holding down the shift button and pressing the mouse button on a
selected row then while holding down everything moving the mouse to an
unselected row. But I resolved this problem by adding these lines to the
mouseup button:

for(int i = 0 ; i < this.dataGridClients.VisibleRowCount; i++) {
this.dataGridClients.UnSelect(i);
}

now it unselects everything before selecting one row.

Erald
 
Dmitriy,

thanks for your concern, I immediately tested it, but when pressing the
shift or control button I can still only select one row....maybe because
of my additional methods at the mouseclick and mousemove events... let
me show them:

private void dataGridClients_MouseMove(object sender, MouseEventArgs e)
{
this.mouseX = e.X; // global variables
this.mouseY = e.Y;
}

private void dataGridClients_Click(object sender,
EventArgs e)
{
System.Windows.Forms.DataGrid.HitTestInfo myHitTest =
this.dataGridClients.HitTest(mouseX,mouseY);
if (myHitTest.Type == DataGrid.HitTestType.RowHeader)
{
for(int i = 0 ; i < this.dataGridClients.VisibleRowCount; i++)
{
this.dataGridClients.UnSelect(i);
}
this.dataGridClients.Select(myHitTest.Row);
}
}

this one I also implemented. But it's for when people navigate through
the datagrid with the arrow keys:

private void dataGridClients_CurrentCellChanged(object sender, EventArgs
e)
{
for(int i = 0 ; i < this.dataGridClients.VisibleRowCount; i++)
{
this.dataGridClients.UnSelect(i);
}
this.dataGridClients.Select(this.dataGridClients.CurrentRowIndex);
}
 
Back
Top