简体   繁体   English

如何在DataGridView C#中单独检查多个复选框

[英]How to check more than one checkbox individually in DataGridView C#

I would like how to achieve this, I tried many of the solutions i found on here on SO but none of them worked我想知道如何实现这一点,我尝试了在 SO 上找到的许多解决方案,但没有一个奏效

在此处输入图片说明

I want to be able to check for example the first and the second option but when i check one of them the others two are unchecked, how can avoid this?我希望能够检查例如第一个和第二个选项,但是当我检查其中一个时,其他两个未选中,如何避免这种情况?

I would like to know what events or properties i need to change to be able to select multiple checkboxes individually no using a master checkbox to select all of them, I hope you can help me thanks.我想知道我需要更改哪些事件或属性才能单独选择多个复选框,而不是使用主复选框来选择所有复选框,希望您能帮助我,谢谢。

I tried this code in the CellClick event of the datagridview我在 datagridview 的CellClick事件中尝试了这段代码

var isChecked = (bool)DGVProductos.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;

if (isChecked)
{
    foreach (DataGridViewRow row in DGVProductos.Rows)
    {
        if (row.Index != e.RowIndex)
        {
            row.Cells["CheckProducto"].Value = !isChecked;
        }
    }
}

I'm not sure what your foreach loop is trying to accomplish, but it is unchecking all of the other boxes that are not the row that was checked.我不确定您的foreach循环试图完成什么,但它取消选中所有不是选中的行的其他框。 If you want the boxes checked individually and to stay checked, remove the foreach loop.如果您希望单独选中这些框并保持选中状态,请删除foreach循环。

Its not clear what you try to achieve there, the code you wrote does exactly the opposite from what you are describing, just remove that code and you should be able to check individual cells.不清楚您尝试在那里实现什么,您编写的代码与您所描述的完全相反,只需删除该代码,您就应该能够检查单个单元格。

Though if you are trying to use that column as a Selection-Indicator, thus the column is not bound to any property, note, that this is not possible, since the column is not bound to the objects checking 1 row-cell will automatically check all rows and vice versa.但是,如果您尝试将该列用作选择指示器,则该列未绑定到任何属性,请注意,这是不可能的,因为该列未绑定到检查 1 行单元格的对象将自动检查所有行,反之亦然。

Side Note: Whatever you want to achieve, code that needs to run when a user check/uncheck a checkbox, should be invoked on CellContentClick event rather then CellClick event.旁注:无论您想实现什么,当用户选中/取消选中复选框时需要运行的代码应该在CellContentClick事件而不是CellClick事件上调用。

I found a solution to what I was trying to achieve I will let the code on here for reference to anyone else facing this same issue我找到了我想要实现的解决方案我将代码放在此处以供其他面临相同问题的人参考

private void DGVProductos_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == 0)
    {
        if ((bool)DGVProductos.SelectedRows[e.ColumnIndex].Cells["CheckProducto"].Value == false)
        {
            DGVProductos.SelectedRows[e.ColumnIndex].Cells["CheckProducto"].Value = true;
        }
        else
        {
            DGVProductos.SelectedRows[e.ColumnIndex].Cells["CheckProducto"].Value = false;
        }
    }    
}

This way i'm able to check more than one checkbox indivually like the question says, not sure if is the best way but this worked for me这样我就可以像问题所说的那样单独检查多个复选框,不确定是否是最好的方法,但这对我有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM