Add mouse drag selection to datagridview

0
0

Would it be possible to add multiselect when the mouse is clicked and dragged like in a normal winforms app. Currently the only way to select a sub section of the grid is to ctrl+shift select. Below is a code snippet of this implemented using cell mouse down, enter, up and move however, this tends to lag when the cells are colored.


int startRowIndex = -1;
int startColIndex = -1;
private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
{
startRowIndex = e.RowIndex;
startColIndex = e.ColumnIndex;
dataGridView1.SetCurrentCell(startColIndex, startRowIndex);
}
}
private void dataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0 && startRowIndex >= 0 && e.ColumnIndex >= 0 && startColIndex >= 0)
{
// Determine the end row index
int endRowIndex = e.RowIndex;
int endColIndex = e.ColumnIndex;
// Select the rows within the range
int minRowIndex = Math.Min(startRowIndex, endRowIndex);
int maxRowIndex = Math.Max(startRowIndex, endRowIndex);
// Select the cols within the range
int minColIndex = Math.Min(startColIndex, endColIndex);
int maxColIndex = Math.Max(startColIndex, endColIndex);
dataGridView1.ClearSelection();
for (int i = minRowIndex; i <= maxRowIndex; i++)
{
for (int j = minColIndex; j <= maxColIndex; j++)
dataGridView1.Rows[i][j].Selected = true;
}
}
}
private void dataGridView1_MouseLeave(object sender, EventArgs e)
{
startRowIndex = -1;
startColIndex = -1;
}
private void dataGridView1_MouseUp(object sender, MouseEventArgs e)
{
startRowIndex = -1;
startColIndex = -1;
}

  • You must to post comments
1
0

I’ve logged this as an enhancement request for a future release.

  • You must to post comments
Showing 1 result
Your Answer

Please first to submit.