| Printable Version
Turning Off Multiple Selection Behavior For DataGrids
By Victor Treckeme
Here is a simple way that will work for most purposes. Since you can't know
which rows are selected without
checking each one, just unselect all of them and set the current row and
selected row to the last row "hit." The
last row will be the one where the mouse is positioned on the MouseUp event.
See the sample code below.
private void dataGrid1_MouseUp (object sender,
System.Windows.Forms.MouseEventArgs e)
// Prevent multiple select when using CTRL or SHIFT keys
{
DataGrid.HitTestInfo hti = dataGrid1.HitTest (e.X, e.Y);
if (e.Button == MouseButtons.Left && hti.Type ==
DataGrid.HitTestType.RowHeader)
{
for (int i = 0; i < dataGrid1.BindingContext[dataGrid1.DataSource,
dataGrid1.DataMember].Count; i++)
{
dataGrid1.UnSelect(i);
}
dataGrid1.CurrentRowIndex = hti.Row;
dataGrid1.Select(hti.Row);
}
}
About the Author: Vic Treckeme is currently an independent consultant with
over eighteen years
experience working in the areas of systems analysis, architecture, software
development, and project
management. He can be reached at victre@msn.com. Copyright (c) 2003, Vic
Treckeme. All rights
reserved.
|