Don't allow user to select multiple rows on a datagrid

  • Thread starter Thread starter Sam
  • Start date Start date
S

Sam

Unlike many who want to be able to select multiple rows, I don't want
the user to be able to select/hi-lite multiple rows on a datagrid.
How would you do this? Is there an option I can turn off?

Thanks...
 
Hey Sam,

This was lifted from here:
http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp#q839q.
See if it works for you.

//*************************
public class MyDataGrid : DataGrid
{
private int oldSelectedRow = -1;

protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs
e)
{
//don't call the base class if left mouse down
if(e.Button != MouseButtons.Left)
base.OnMouseMove(e);
}

protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs
e)
{
//don't call the base class if in header
DataGrid.HitTestInfo hti = this.HitTest(new Point(e.X, e.Y));
if(hti.Type == DataGrid.HitTestType.Cell)
{
if(oldSelectedRow > -1)
this.UnSelect(oldSelectedRow);
oldSelectedRow = -1;
base.OnMouseDown(e);
}
else if(hti.Type == DataGrid.HitTestType.RowHeader)
{
if(oldSelectedRow > -1)
this.UnSelect(oldSelectedRow);
if((Control.ModifierKeys & Keys.Shift) == 0)
base.OnMouseDown(e);
else
this.CurrentCell = new DataGridCell(hti.Row,
hti.Column);
this.Select(hti.Row);
oldSelectedRow = hti.Row;
}
}
}
 
Back
Top