We have a very strange problem in Windows Form that we cannot understand.
Our Windows Form has a DataGridView with a DataGridViewCheckBoxColumn in the first column.

We have added the following functionality that allows the user to shift-> click to select multiple rows in this grid:
int colHit = gvLibrary.HitTest(e.X, e.Y).ColumnIndex;
int lastRowHit;
if (e.Button == MouseButtons.Left)
{
if (colHit == 0)
{
if (Control.ModifierKeys == Keys.Shift)
{
lastRowHit = gvLibrary.HitTest(e.X, e.Y).RowIndex;
ShiftClickCheckBoxSetter(this.gvLibrary, int.Parse(txtFirstClickRow.Text), lastRowHit);
}
else
{
int firstRowHit = gvLibrary.HitTest(e.X, e.Y).RowIndex;
txtFirstClickRow.Text = firstRowHit.ToString();
}
}
}
Here's the CheckBoxSetter code:
private void ShiftClickCheckBoxSetter(DataGridView dataGridView, int p, int lastRowHit)
{
if (p < lastRowHit)
{
for (int i = p; i < lastRowHit; i++)
{
dataGridView.Rows[i].Cells[0].Value = true;
}
}
else
{
for (int i = p; i >= lastRowHit; i--)
{
dataGridView.Rows[i].Cells[0].Value = true;
}
}
}
And it works as expected.

We also added ContextMenuStrip to the control for the right-click event.
else if (e.Button == MouseButtons.Right)
{
if (colHit != 0)
{
ContextMenuStrip m = new ContextMenuStrip();
m.Items.Add("Select All", null, m_LibraryItemClicked);
m.Items.Add("Select None", null, m_LibraryItemClickedNone);
m.Show(gvLibrary, e.Location);
}
}
Delegate Event 1:
void m_LibraryItemClicked(object sender, EventArgs e) {
foreach (DataGridViewRow dgvr in gvLibrary.Rows)
{
if (dgvr.Selected) {
dgvr.Selected = false;
}
dgvr.Cells["LSelect"].Value = true;
}
}
Delegate the second event:
private void m_LibraryItemClickedNone(object sender, EventArgs e)
{
foreach (DataGridViewRow dgvr in gvLibrary.Rows)
{
if (dgvr.Selected)
dgvr.Selected = false;
dgvr.Cells["LSelect"].Value = false;
}
}
This allows the user to select all or select none for the checkboxes.

When the "Select All" option is selected, all the checkboxes are checked:

However, if the "Select None" option is selected:

, , Shift-Click:

, Grid , , IE:
private void m_LibraryItemClickedNone(object sender, EventArgs e)
{
foreach (DataGridViewRow dgvr in gvLibrary.Rows)
{
if (dgvr.Selected)
dgvr.Selected = false;
dgvr.Cells["LSelect"].Value = false;
}
}
, , - , .
.