DataGridView使用CTRL和Shift键检查多个复选框

发布于 2025-01-23 08:30:01 字数 1393 浏览 3 评论 0原文

我在另一个论坛上发现了完全相同的问题(下图)。不幸的是,没有解决方案。我是这个论坛的新手,并提交了另外两篇文章,但可悲的是没有人回应他们。希望这个人更多地关注。提前致谢。

“我有一个datagridview,第0列是一个复选框。我已经启用了多电选择,因此我可以使用shift and ctrl选择行,但无法使我的复选框正确启用和禁用。我希望能够选择行然后,一旦选择了单击第0列,并检查了所有行,但是当我这样做时,我的选择就会放松。

我还尝试了CellClick事件,这是我的偏爱。

我的代码如下:

        private void dgv_Off_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.RowIndex == -1) return;
        dgv_On.ClearSelection();
        int colindex = dgv_Off.CurrentCell.ColumnIndex;
        if (dgv_Off.SelectedRows.Count > 1)
        {
            foreach (DataGridViewRow dgvOff in dgv_Off.SelectedRows)
            {
                dgv_Off.Focus();
                if (colindex == 0)
                {
                    bool IsChecked = Convert.ToBoolean(dgvOff.Cells[0].Value);
                    if (IsChecked == true)
                    {
                        dgvOff.Cells[0].Value = false;
                        dgv_Off.ClearSelection();
                        dgv_Off.Update();
                    }
                    else
                    {
                        dgvOff.Cells[0].Value = true;
                        dgv_Off.ClearSelection();
                        dgv_Off.Update();
                    }
                }
            }
        }
        dgv_Off.CommitEdit(DataGridViewDataErrorContexts.Commit);
    }

I found the exact same question (below) on another forum. Unfortunately there was no solution given. I'm new to this forum and have submitted 2 other posts but sadly no one has responded to them. Hopefully this one garners more attention. Thanks in advance.

"I have a DataGridView, column 0 is a checkbox. I've enabled multiselect so I can select the rows with Shift and Ctrl but can't get my check boxes to enable and disable correctly. I want to be able to select the rows and then once selected click on column 0 and have all the rows checked or unchecked, but what is happening is I loose my selection when I do this. Currently everything is Read only except for column 0 and I have FullRowSelect enabled."

I've also tried the CellClick event which is my preference.

My code is as follows:

        private void dgv_Off_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.RowIndex == -1) return;
        dgv_On.ClearSelection();
        int colindex = dgv_Off.CurrentCell.ColumnIndex;
        if (dgv_Off.SelectedRows.Count > 1)
        {
            foreach (DataGridViewRow dgvOff in dgv_Off.SelectedRows)
            {
                dgv_Off.Focus();
                if (colindex == 0)
                {
                    bool IsChecked = Convert.ToBoolean(dgvOff.Cells[0].Value);
                    if (IsChecked == true)
                    {
                        dgvOff.Cells[0].Value = false;
                        dgv_Off.ClearSelection();
                        dgv_Off.Update();
                    }
                    else
                    {
                        dgvOff.Cells[0].Value = true;
                        dgv_Off.ClearSelection();
                        dgv_Off.Update();
                    }
                }
            }
        }
        dgv_Off.CommitEdit(DataGridViewDataErrorContexts.Commit);
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

习惯成性 2025-01-30 08:30:01

我并不是要完全遵循您的意思……“我希望能够选择行,然后选择一旦选择单击第0列,然后检查或未检查所有行,…“ …? …您将如何确定是否应检查选定的行?我的意思是,如果选定的行已经检查并选择了未选中的值……那么您如何知道要设置所有复选框的值?

我将假设您想将“选定”行复选框值更改为当前拥有的“相反”值。换句话说,在选择的行中,我们将循环遍历所有选定的复选框,并仅对选定的行否定复选框值。如果是这种情况,那么下面的代码应该执行此操作。

我建议您使用网格columnheadermouseclick事件。当单击列标头为零(0)列时,然后循环遍历所有选定的行,并否定当前具有的任何值。像下面的东西…

private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) {
  if (e.ColumnIndex == 0) {
    foreach (DataGridViewRow row in dataGridView1.SelectedRows) {
      if (row.Cells[0].Value != null) {
        row.Cells[0].Value = !(bool)row.Cells[0].Value;
      }
      else {
        row.Cells[0].Value = true;
      }
    }
  }
}

I am not following exactly what you mean by… “I want to be able to select the rows and then once selected click on column 0 and have all the rows checked or unchecked, … “ … ? … How would you determine if the selected rows should be checked or not-checked? I mean if the selected rows have checked and unchecked values selected… then how would you know what value to set ALL the check boxes?

I will assume you want to change the “selected” rows check box values to the “opposite” value it currently has. In other words, with the rows selected we would loop through all the selected check boxes and negate the check box values for only the selected rows. If this is the case then the code below should do this.

I suggest you use the grids ColumnHeaderMouseClick event. When the clicked column header is column zero (0), then loop through all the selected rows and negate whatever value the check box currently has. Something like below…

private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) {
  if (e.ColumnIndex == 0) {
    foreach (DataGridViewRow row in dataGridView1.SelectedRows) {
      if (row.Cells[0].Value != null) {
        row.Cells[0].Value = !(bool)row.Cells[0].Value;
      }
      else {
        row.Cells[0].Value = true;
      }
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文