Turning Off Multiple Selection Behavior For DataGrids
Here is a simple way that will work for most purposes. Since you can't know which rows are selected withoutchecking each one, just unselect all of them and set the current row and selected row to the last row "hit." Thelast 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 yearsexperience working in the areas of systems analysis, architecture, software development, and projectmanagement. He can be reached at victre@msn.com. Copyright (c) 2003, Vic Treckeme. All rightsreserved.












No comments yet... Be the first to leave a reply!