Thursday, July 31, 2008

Multiple Select Grid Row

To select row of grid on click of any cell

Put one datagrid and checkbox on the form, check checkbox to enable selection using control.

Add mouseup eventhandler

this.dgSelect.MouseUp += new System.Windows.Forms.MouseEventHandler(this.dgSelect_MouseUp);

Add the below method of mouseup event

private void dgSelect_MouseUp(object sender, MouseEventArgs e)
{
if(chkCtrl.Checked)
{
if(System.Windows.Forms.Control.ModifierKeys == Keys.Control)
{
select();
}
else
{
for(int j = 0; j < dgRow ; j++)
dgSelect.UnSelect(j);
selectedRow.Clear();
dgSelect.Select(dgSelect.CurrentCell.RowNumber);
selectedRow.Add(dgSelect.CurrentCell.RowNumber);
}
}
else
{
if(!(System.Windows.Forms.Control.ModifierKeys == Keys.Control))
{
select();
}
else
{
for (int i = 0; i < selectedRow.Count; i++)
{
dgSelect.Select(int.Parse(selectedRow[i].ToString()));
}
}
}
}

Add below methods to select

private void select()
{
int c = dgSelect.CurrentRowIndex;
if(selectedRow.Contains(c))
{
dgSelect.UnSelect(c);
selectedRow.Remove(c);
}
else
{
dgSelect.Select(c);
selectedRow.Add(c);
}
for (int i = 0; i < selectedRow.Count; i++)
{
dgSelect.Select(int.Parse(selectedRow[i].ToString()));
}
}

check checkbox to enable selection using control, uncheck it to select without control

Happy Coding :)

No comments:

Site Meter