M
Matthijs de Z
Hi,
I would like to be able to multi select rows in a datagridview, just
by clicking a cell in the rows I need.
The code below, does work. But it looks like it might not be the most
efficient way to do this. When I run the program and select rows, the
datagrid does not really instantly change, but you can see it
changing. It's kind of slow, while my machine is not slow at all. Is
there a more efficient way to do this? Can I let the datagrid stop
from updating, till the foreach loop is done and then start updating
again? If so, how can I stop the datagrid from updating / refreshing?
Kind regards,
Matthijs
private List<int> RowIndexTracker = new List<int>();
private void dataGridView1_CellMouseClick(object sender,
DataGridViewCellMouseEventArgs e)
{
if (e.RowIndex >= 0)
{
if (RowIndexTracker.Contains(e.RowIndex))
{
RowIndexTracker.Remove(e.RowIndex);
dataGridView1.Rows[e.RowIndex].Selected = false;
}
else
{
RowIndexTracker.Add(e.RowIndex);
}
foreach (int index in RowIndexTracker)
{
dataGridView1.Rows[index].Selected = true;
}
dataGridView1.Enabled = true;
}
}
I would like to be able to multi select rows in a datagridview, just
by clicking a cell in the rows I need.
The code below, does work. But it looks like it might not be the most
efficient way to do this. When I run the program and select rows, the
datagrid does not really instantly change, but you can see it
changing. It's kind of slow, while my machine is not slow at all. Is
there a more efficient way to do this? Can I let the datagrid stop
from updating, till the foreach loop is done and then start updating
again? If so, how can I stop the datagrid from updating / refreshing?
Kind regards,
Matthijs
private List<int> RowIndexTracker = new List<int>();
private void dataGridView1_CellMouseClick(object sender,
DataGridViewCellMouseEventArgs e)
{
if (e.RowIndex >= 0)
{
if (RowIndexTracker.Contains(e.RowIndex))
{
RowIndexTracker.Remove(e.RowIndex);
dataGridView1.Rows[e.RowIndex].Selected = false;
}
else
{
RowIndexTracker.Add(e.RowIndex);
}
foreach (int index in RowIndexTracker)
{
dataGridView1.Rows[index].Selected = true;
}
dataGridView1.Enabled = true;
}
}