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

标签 c# asp.net visual-studio gridview

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

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

最佳答案

您不需要在 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;
            }
        }
    }

有关更多详细信息,请查看 system.web.ui.webcontrols.gridview.rowdatabound

关于c# - 为什么 foreach 循环在 gridview 的最后一行失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33050991/

相关文章:

asp.net - Comet/Reverse AJAX 的现代实现如何工作?任何稳定的 C# WCF 或 ASP.NET 实现?

c# - 如何在 ASP.NET MVC 的 Controller 中获取传入的 InputStream

c# - 测试函数内部或外部是否需要更好?

c# - 在构建过程中运行 c# 控制台应用程序

c# - yield return 语句中静态成员的初始化顺序

c# - 销毁 dataContext 与保持打开以供将来数据库访问的性能考虑?

c# - Android - 使用 bool 值选中 'Custom ListView' 中的复选框

c# - UWP 中一个 CS 文件的不同 XAML 布局结构

c++ - 强制自动并行化 VS 2012

visual-studio - 代码片段: Auto complete for ArgumentNullException in Visual Studio