Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/8.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
C# 为什么foreach循环在gridview的最后一行失败?_C#_Asp.net_Visual Studio_Gridview - Fatal编程技术网

C# 为什么foreach循环在gridview的最后一行失败?

C# 为什么foreach循环在gridview的最后一行失败?,c#,asp.net,visual-studio,gridview,C#,Asp.net,Visual Studio,Gridview,我已经添加了一个代码来给GRIDVIEW单元格的背景上色#如果单元格的文本为14!=“nbsp;”除最后一行外,它确实有效。即使最后一行不等于“nbsp;”,它也不会着色 您不需要在RowDataBound事件中循环行,只需使用e对象引用每一行即可 protected void grdviewCases_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataContro

我已经添加了一个代码来给GRIDVIEW单元格的背景上色#如果单元格的文本为14!=“nbsp;”除最后一行外,它确实有效。即使最后一行不等于“nbsp;”,它也不会着色


您不需要在RowDataBound事件中循环行,只需使用
e
对象引用每一行即可

protected void grdviewCases_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow) 
        {
            if (e.Row.Cells[14].Text != " ")
            {
                e.Row.Cells[14].BackColor = Color.Red; ;
                e.Row.Cells[14].ForeColor = Color.WhiteSmoke;
            }
        }
    }

有关更多详细信息,请检查

可能只有在RowDataBound事件发生后才会将新行添加到Rows集合中。我会先检查一下。另外,为什么在每一行数据绑定后迭代整个rows集合,而不是只在新绑定的行上工作?一个简单的解决方案是消除foreach指令,并在剩余的代码中用e.row替换gr
protected void grdviewCases_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow) 
        {
            if (e.Row.Cells[14].Text != " ")
            {
                e.Row.Cells[14].BackColor = Color.Red; ;
                e.Row.Cells[14].ForeColor = Color.WhiteSmoke;
            }
        }
    }