Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Winforms 如何知道复选框是否选中_Winforms_Visual Studio 2008_C# 3.0_Datagridviewcheckboxcell - Fatal编程技术网

Winforms 如何知道复选框是否选中

Winforms 如何知道复选框是否选中,winforms,visual-studio-2008,c#-3.0,datagridviewcheckboxcell,Winforms,Visual Studio 2008,C# 3.0,Datagridviewcheckboxcell,在数据网格视图中,我需要在行上循环并获取包含复选框的行 dgv.rows[i]。单元格[0]。在这两种情况下,值都返回空 所有这些都发生在事件CellContentClick上,请尝试: 'VB Dim MyCheckBox As CheckBox = _ CType(dgv.rows[i].cells[0].findcontrol("checkbox_id"), CheckBox) C: 当单元格不包含任何其他控件时,单元格上的Value属性引用文本内容。如果复选框不包含任何数据,则

在数据网格视图中,我需要在行上循环并获取包含复选框的行 dgv.rows[i]。单元格[0]。在这两种情况下,值都返回空 所有这些都发生在事件CellContentClick上,请尝试:

'VB
Dim MyCheckBox As CheckBox = _
    CType(dgv.rows[i].cells[0].findcontrol("checkbox_id"), CheckBox)
C:


当单元格不包含任何其他控件时,单元格上的Value属性引用文本内容。

如果复选框不包含任何数据,则结果将为空值。您可以使用bool.parse解析循环中的值,假设该值不为null,即

for ( int i = 0; i < dgv.Rows.Count; i++ )
{
    var val = dgv.Rows[i].Cells[0].Value;
    if ( val == null ) { continue; }

    bool isChecked = bool.Parse( val.ToString() );
}

问题在C代码中,您的答案是针对VB的。对于信息,您不必使用findControl。您可以使用checkboxdgv.rows[ind].cells[0]。children[0]Metro。我已经说过dgv.rows[I].cells[0]。值不起作用。它在这两个字段中都返回空字符串cases@Mario-您还说了dgv.rows[ind].cells[0].children[0]引用您对@mellamokb的评论是否有效;Cells属性上没有名为Children的此类属性。如果该值是空字符串,那么无论您在绑定中使用什么,都会将该值设置为空。你应该发布所有相关代码。请仔细阅读VB代码中存在的属性子项的注释。在我尝试了很多事情之后,我发现问题出在事件上,因为当我使用另一个事件时,该值为true。你知道当我更改复选框的复选框时会触发另一个事件吗?你需要发布一些代码。根据您提供的信息,我们都提供了有效的答案。如果没有相关的代码,我们就无法做其他事情。
for ( int i = 0; i < dgv.Rows.Count; i++ )
{
    var val = dgv.Rows[i].Cells[0].Value;
    if ( val == null ) { continue; }

    bool isChecked = bool.Parse( val.ToString() );
}
static class DataGridViewExtensions
{
    public static IEnumerable<DataGridViewRow> CheckedRows(this DataGridView dgv, string checkedColumnName)
    {
        return CheckedRows(dgv, dgv.Columns[checkedColumnName].Index);
    }

    public static IEnumerable<DataGridViewRow> CheckedRows(this DataGridView dgv, int checkedColumnIndex)
    {
        foreach (DataGridViewRow row in dgv.Rows)
        {
            DataGridViewCheckBoxCell cell = row.Cells[checkedColumnIndex] as DataGridViewCheckBoxCell;
            Debug.Assert(cell != null, "The column specified is not a check box column");
            if (cell != null && (bool)cell.Value)
                yield return row;
        }
    }
}