如何验证是否选中了

如何验证是否选中了

本文介绍了如何验证是否选中了 DataGridViewCheckBoxCell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我已将数据表绑定到 DataGridView,该数据表有一个名为Status"的列,其类型为 Boolean.我可以通过代码将值设置为 truefalse 就好了.

I have bound a data table to a DataGridView, this data table has a column called "Status" which is of type Boolean. I can set the value to true or false just fine via code.

但是,我不知道如何检查给定的行是否已被检查.这是我尝试使用的代码,编译后显示错误指定的强制转换无效".

However, I can't figure out how to check to see if the given row is already checked or not. This is the code I am trying to use and compiling it shows the error "the specified cast is invalid".

任何帮助将不胜感激.

if (rowIndex >= 0)
{
    var cbxCell = (DataGridViewCheckBoxCell)dgvScan.Rows[rowIndex].Cells["Status"];

    if ((bool)cbxCell.Value)
    {
        // Do stuff
    }
    else
    {
        // Do other stuff
    }
}

推荐答案

谢谢大家.有同样的问题,但我发现在检查值之前编写 senderGrid.EndEdit() 可以解决它.

Thank you all.Had the same problem but i find out that writing senderGrid.EndEdit(), before checking the value, resolves it.

private void dgvRiscos_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        var senderGrid = (DataGridView)sender;
        senderGrid.EndEdit();

        if (senderGrid.Columns[e.ColumnIndex] is DataGridViewCheckBoxColumn &&
            e.RowIndex >= 0)
        {

            var cbxCell = (DataGridViewCheckBoxCell)senderGrid.Rows[e.RowIndex].Cells["associado"];
            if ((bool)cbxCell.Value)
            {
                   // Criar registo na base de dados
            }
            else
            {
                   // Remover registo da base de dados
            }
        }
    }

继续努力

这篇关于如何验证是否选中了 DataGridViewCheckBoxCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 21:34