DataGridView事件检查更改 [英] DataGridView event for checked change

查看:225
本文介绍了DataGridView事件检查更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 DataGridView ,它有一堆列加上一个名为 IsChecked 的checkBoxColumn。

I have a DataGridView which has a bunch of columns plus a checkBoxColumn called IsChecked.

每当行的检查状态发生变化时,我想提出一个事件。当用户单击复选框时,或者当他们按一下空格键时,可能会检查或取消选中一行。

I want to raise an event whenever a row's checked state changes. Checking or Unchecking a row may be done either when user clicks the checkBox or when they press space key on a row.

这是我如何将checkBoxColumn添加到我的网格:

this is how I added checkBoxColumn to my grid:

dgvMain.Columns.Add(new DataGridViewCheckBoxColumn { 
            Name = "IsChecked" , 
            Width = 20, 
            Visible = false,
            HeaderText = "",
            SortMode = DataGridViewColumnSortMode.NotSortable,
            DisplayIndex = Columns.Count, //to be displayed as last column
            ValueType = typeof(bool),
            FalseValue = false,
            TrueValue = true
        });

这是我按空格键检查单元格的方式:

and this is how I check the cell when space key is pressed:

private void dgvMain_KeyDown(object sender, KeyEventArgs e)
{
     foreach (DataGridViewRow row in dgvMain.SelectedRows)
     {
         bool? checkState = (bool?)row.Cells["IsChecked"].Value;
         if (checkState == null || checkState == false)
             checkState = true;
         else
             checkState = false;
         row.Cells["IsChecked"].Value = checkState;
     }

}

尝试#1:

我尝试使用 CellEndEdit 事件,只有当您使用鼠标检查单元格时有帮助,但是当我按空格,单元格检查/取消选中 CellEndEdit 不触发。

Attempt # 1:
I tried using CellEndEdit event which only helped when you check the cell using mouse, but when I press space and the cell checks/unchecks CellEndEdit does not fire.

尝试#2:

我尝试使用 CellValueChanged 事件,当我按空格时工作正常,当我使用鼠标检查框并留下行时,,但是当我多次检查并取消选中该框时,没有任何反应。

Attempt # 2:
I tried using CellValueChanged event, which works fine when I press space, and also when I check the box using mouse and leave the row but when I check and uncheck the box multiple times, nothing happens. It seems like CellValueChanged raises when the cell is finished editing by mouse.

尝试#3:
$ CellValueChanged b $ b我也尝试使用 CurrentCellDirtyStateChanged ,其响应由鼠标引起的第一次检查的更改,但不响应快速检查的更改鼠标,只有第一个,当您离开行。 我想抓住所有检查的更改,即使用户通过点击复选框快速更改已检查的状态,也是这样。

Attempt # 3:
I Also tried using CurrentCellDirtyStateChanged which responds to first checked change caused by mouse but does not respond to rapid checked changes bu mouse, only the first one and also when you leave the row. I want to catch all checked changes, even if user changes the checked state rapidly by clicking on the checkBox, that counts too.

不知道是否有任何事件已经为此目的。如果没有,我如何以编程方式为此列添加一个eventHandler?

I'm not sure if there is any event already for this purpose or not. If not, how can I add an eventHandler for this column programmatically?

推荐答案

CurrentCellDirtyStateChanged 事件处理程序,触发器 EndEdit ,以便触发值更改的事件。

In the CurrentCellDirtyStateChanged event handler, trigger EndEdit so that the value changed event is triggered.

void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
    if (dataGridView1.IsCurrentCellDirty && dataGridView1.CurrentCell.ColumnIndex == CheckColumnIndex)
    {
        dataGridView1.EndEdit();
    }
}

CellValueChanged 事件处理程序从CheckBoxColumn获取值。

In the CellValueChanged event handler get the value from the CheckBoxColumn.

void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == CheckColumnIndex)
    {
        DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
        bool achecked = Convert.ToBoolean(checkCell.Value);
    }
}

这篇关于DataGridView事件检查更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆