c# - ASP.NET GridView 在 BoundField 上使用 FindControl() 来操作字段

标签 c# asp.net css gridview boundfield

我正在使用一个旧应用程序,该应用程序具有用于不同位置的硬编码列,现在正在添加新位置我决定尝试动态填充。该应用程序的一项功能是在状态被视为“不良”时显示红色文本和粗体文本。这是通过使用带有 TemplateFields 的选定行中的单元格中的“FindControl()”函数执行的。

既然我已经将其设置为使用绑定(bind)字段,那么我将如何在 DataBound 事件期间更改文本颜色、大小等?

BoundField 被添加到 GridView

        BoundField statusField = new BoundField();
        statusField.DataField = "ExceptionStatusCode";
        statusField.HeaderText = "Status";
        statusField.SortExpression = "ExceptionStatusCode";
        this.gvView.Columns.Add(statusField);
        

GridView 的数据绑定(bind)事件

    protected void gvView_DataBound(object sender, EventArgs e)
    {
        foreach (GridViewRow row in this.gvView.Rows)
        {
            //NO LONGER WORKS, NEED TO KNOW HOW TO REPRODUCE
            //WHAT IS BELOW FOR BOUND FIELD
            Label lblPartStatus = ((Label) row.Cells[StatusColumn].FindControl("lblPartStatus"));
            if (lblPartStatus.Text == "BAD")
            {
                lblPartStatus.ForeColor = System.Drawing.Color.Red;
                row.ToolTip = "One or more locations is missing information!";
                row.BackColor = System.Drawing.Color.Salmon;
            }
        }
    }

最佳答案

几年前,我编写了一些代码来帮助您使用列的 SortExpression、HeaderText 或 DataField 查找单元格索引。多年来,它为我节省了大量精力,您只需像 myRow.Cells[myRow.GetCellIndexByFieldHandle(myDataFieldName)]

那样调用它
public static class Utility
{
    /// <summary>
    /// Gets the ordinal index of a TableCell in a rendered GridViewRow, using a text fieldHandle (e.g. the corresponding column's DataFieldName/SortExpression/HeaderText)
    /// </summary>
    public static int GetCellIndexByFieldHandle(this GridView grid, string fieldHandle)
    {
        int iCellIndex = -1;

        for (int iColIndex = 0; iColIndex < grid.Columns.Count; iColIndex++)
        {
            if (grid.Columns[iColIndex] is DataControlField)
            {
                DataControlField col = (DataControlField)grid.Columns[iColIndex];
                if ((col is BoundField && string.Compare(((BoundField)col).DataField, fieldHandle, true) == 0)
                    || string.Compare(col.SortExpression, fieldHandle, true) == 0
                    || col.HeaderText.Contains(fieldHandle))
                {
                    iCellIndex = iColIndex;
                    break;
                }
            }
        }
        return iCellIndex;
    }

    /// <summary>
    /// Gets the ordinal index of a TableCell in a rendered GridViewRow, using a text fieldHandle (e.g. the corresponding column's DataFieldName/SortExpression/HeaderText)
    /// </summary>
    public static int GetCellIndexByFieldHandle(this GridViewRow row, string fieldHandle)
    {
        return GetCellIndexByFieldHandle((GridView)row.Parent.Parent, fieldHandle);
    }
}

获得单元格后,我建议您通过设置 Cell.CssClass 来操作它,然后使用 CSS 对其进行相应的样式设置。避开内联样式,这是您在设置 ForeColorBackColor 等时得到的。

关于c# - ASP.NET GridView 在 BoundField 上使用 FindControl() 来操作字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6773227/

相关文章:

css - 如何在悬停时定位前一个 sibling ?

php - 未选中复选框时的Jquery目标父节点

c# - 通过在 SQL 表的多列中拆分字符串来搜索?

c# - 如何将字符串传递给 Angular 中的发布请求?

c# - 总计 Gridview 上的列并显示在页脚中

c# - 通用处理程序不返回值

javascript - 如何从代码隐藏中读取JavaScript设置的文本框文本?

c# - 通过 ActiveSync 在 Windows Mobile 上运行应用程序

c# - 以编程方式创建文档 Kentico 9

css - Bootstrap 不工作