vendredi 24 juin 2016

Windows Form->DataGridView->DataGridViewCheckBoxColumn Uncheck All Leaves One Item Checked

We have a very strange problem in a Windows Form that we cannot seem to figure out.

Our Windows Form has a DataGridView with a DataGridViewCheckBoxColumn in the first column.

enter image description here

We've added a the following functionality that allows a 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 this is working as expected.

enter image description here

We've also added a ContextMenuStrip to the control for a 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 One:

     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 Event Two:

 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 to the user to select all or select none for the checkboxes.

Select All/Select None

When the Select All selection is chosen, all check boxes are checked:

All Selected

However when the Select None option is selected:

Select None

All check boxes are de-selected, except for the last one checked in the Shift-Click event:

All But One

I would think that iterating through all of the Grid Rows and setting the checkbox to not selected would suffice, 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;
        }
    }

However there seems to be some kind of state property that is disallowing this checkbox in that row to be changed.

Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire