数据网格复选框列选择无法正常工作

发布于 2024-12-11 11:52:49 字数 645 浏览 2 评论 0原文

我想要一个带有复选框的列,当用户单击它们时,他们会选择自己的行(突出显示它)。我已经想出了这段代码,但不能完成工作,我该如何修复它?

有更好的方法吗? (即使在我“取消选中”复选框后,该行仍保持突出显示)。

 private void dataGrid_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0 && e.RowIndex != -1) 
            {
                if (Convert.ToBoolean(dataGrid.Rows[e.RowIndex].Cells[0].Value) == true)
                    dataGrid.Rows[e.RowIndex].Selected = false;
                else if (Convert.ToBoolean(dataGrid.Rows[e.RowIndex].Cells[0].Value) == false)
                    dataGrid.Rows[e.RowIndex].Selected = true;
            }
        }

I want to have a column with checkboxes that when ever the user clicks them, they select their own row (hightlight it). I have come up with this code, with does not do the job, how can I fix it?

Is there a better way of doing this? (The row stays highlighter even after I "uncheck" the checkbox).

 private void dataGrid_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0 && e.RowIndex != -1) 
            {
                if (Convert.ToBoolean(dataGrid.Rows[e.RowIndex].Cells[0].Value) == true)
                    dataGrid.Rows[e.RowIndex].Selected = false;
                else if (Convert.ToBoolean(dataGrid.Rows[e.RowIndex].Cells[0].Value) == false)
                    dataGrid.Rows[e.RowIndex].Selected = true;
            }
        }

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

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

发布评论

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

评论(2

杀手六號 2024-12-18 11:52:49

尝试将逻辑放入 CellMouseUp 事件处理程序中,因为 CellClick 事件在 CheckBox 状态更新之前发生。

这与使用 EditedFormattedValue属性(包含单元格的当前格式化值)来检索 CheckBoxes 当前状态。

来自 MSDN

Value 属性是单元格包含的实际数据对象,
而 FormattedValue 是该值的格式化表示
目的。

它存储单元格当前的格式化值,无论是否
单元格处于编辑模式并且该值尚未提交。

这是一个工作示例。

void dataGrid_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
{
    if (e.ColumnIndex == 0 && e.RowIndex != -1)
    {
        DataGridViewCheckBoxCell checkBoxCell =
            dataGrid.Rows[e.RowIndex].Cells[0] as DataGridViewCheckBoxCell;

        if (checkBoxCell != null)
        {
            dataGrid.Rows[e.RowIndex].Selected = Convert.ToBoolean(checkBoxCell.EditedFormattedValue);
        }
    }
}

希望这有帮助。

Try placing the logic in a CellMouseUp event handler as the CellClick event is occurring before the CheckBox state has been updated.

This along with using the EditedFormattedValue property (which contains the current, formatted value of the cell) to retrieve the CheckBoxes current state.

From MSDN:

The Value property is the actual data object contained by the cell,
whereas the FormattedValue is the formatted representation of this
object.

It stores the the current, formatted value of the cell, regardless of whether
the cell is in edit mode and the value has not been committed.

Here is a working example.

void dataGrid_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
{
    if (e.ColumnIndex == 0 && e.RowIndex != -1)
    {
        DataGridViewCheckBoxCell checkBoxCell =
            dataGrid.Rows[e.RowIndex].Cells[0] as DataGridViewCheckBoxCell;

        if (checkBoxCell != null)
        {
            dataGrid.Rows[e.RowIndex].Selected = Convert.ToBoolean(checkBoxCell.EditedFormattedValue);
        }
    }
}

Hope this helps.

青衫儰鉨ミ守葔 2024-12-18 11:52:49

CellMouseUp 不适用于按空格键进行选择。
如果您不必进行“真实”选择,我会在单元格值更改时更改行背景颜色,这会更容易:

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == 0 && e.RowIndex != -1)
    {
        if (Convert.ToBoolean(dataGridView1.Rows[e.RowIndex].Cells[0].Value) == true)
            dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Blue;
        else if (Convert.ToBoolean(dataGridView1.Rows[e.RowIndex].Cells[0].Value) == false)
            dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.White;
    }
}

CellMouseUp will not work for selection with SPACE press.
If you don't have to do "real" selection, I'd change row background color on cell value change, it'd be much easier:

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == 0 && e.RowIndex != -1)
    {
        if (Convert.ToBoolean(dataGridView1.Rows[e.RowIndex].Cells[0].Value) == true)
            dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Blue;
        else if (Convert.ToBoolean(dataGridView1.Rows[e.RowIndex].Cells[0].Value) == false)
            dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.White;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文