c# - 从 DataGridViewCheckBoxCell 获取值

标签 c# winforms

我正在开发一个名为 ListingGridDataGridView,试图激活/停用已在内部的任何 DataGridViewCheckBoxCell 上“检查”的用户DataGridViewCheckBoxColumn

这就是我尝试这样做的方式:

foreach (DataGridViewRow roow in ListingGrid.Rows)
{
    if ((bool)roow.Cells[0].Value == true)
    {
        if (ListingGrid[3, roow.Index].Value.ToString() == "True")
        {
            aStudent = new Student();
            aStudent.UserName = ListingGrid.Rows[roow.Index].Cells[2].Value.ToString();
            aStudent.State = true;
            studentList.Add(aStudent);

        }
    }
}

据我所知,当您检查 DataGridViewCheckBoxCell 时,单元格的值为 true 对吗?但它不允许我将值转换为 bool 然后比较它,抛出一个无效的转换异常。

最佳答案

尝试:

DataGridViewCheckBoxCell chkchecking = roow.Cells[0] as DataGridViewCheckBoxCell;

    if (Convert.ToBoolean(chkchecking.Value) == true)
{
}

DataGridViewCheckBoxCell chkchecking = roow.Cells[0] as DataGridViewCheckBoxCell;

        if ((bool)chkchecking.Value == true)
    {
    }

关于c# - 从 DataGridViewCheckBoxCell 获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13632536/

相关文章:

c# - 未命中 IEqualityComparer + GroupBy 的 GetHashCode 中的断点

sql-server - 保护连接字符串免受中间人的影响

c# - C#如何确保完整性?(DLL未修改)

c# - 如何打开一个新的独立窗体?

c# - 带有 Windows 7 拖动增强功能的无边框窗体

c# - C# 中的计时器在打开程序后触发 X 秒?

c# - 是否可以捕获并处理网站卸载事件 - asp.net 网页 razor 2?

C# 如何使用标准值初始化包含 list<T> 的对象

c# - 如何创建具有多个 XAML 文件的 WPF 应用程序?

c# - IE VS Chrome 和 Firefox 中的网络安全(错误)