Windows Form-> DataGridView-> DataGridViewCheckBoxColumn Uncheck all sheets One item checked

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.

enter image description here

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;
        //mouse left click
        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.

enter image description here

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.

Select All / Select None

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

All selected

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

Choose None

, , Shift-Click:

All but one

, 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;
        }
    }

, , - , .

.

+4
1

. , , ( ). , .

, dataGridView1.CurrentCell = null; "LSelect". .

private void m_LibraryItemClickedNone(object sender, EventArgs e)
{
    dataGridView1.CurrentCell = null;    
    foreach (DataGridViewRow dgvr in gvLibrary.Rows)
    {
        if (dgvr.Selected)
           dgvr.Selected = false;

        dgvr.Cells["LSelect"].Value = false;
    }
}
+1

All Articles