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

时间:2011-07-21 08:27:44

标签: c# asp.net css gridview boundfield

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

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

BoundField被添加到GridView

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

GridView的DataBound事件

    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;
            }
        }
    }

3 个答案:

答案 0 :(得分:4)

多年前,我编写了一些代码来帮助您使用列的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等时获得的内容。

答案 1 :(得分:2)

如果你的网格中总是有X列,你可以像这样访问它们:

void grid_RowDataBound(Object sender, GridViewRowEventArgs e)
  {

    if(e.Row.RowType == DataControlRowType.DataRow)
    {
      string text = e.Row.Cells[1].Text;

      if( text.Equals("BAD") )
      {
         //do your stuff...
      }
    }

  }

现在将单元格索引更改为您感兴趣的索引列... 将函数附加到OnRowDataBound事件而不是OnDataBound

答案 2 :(得分:-1)

原始解决方案是将列索引添加到ViewState ...

statusField.SortExpression = "ExceptionStatusCode";
ViewState("StatusIndex") = this.gvView.Columns.Count;
this.gvView.Columns.Add(statusField);

...然后在数据绑定上再次使用它

Label lblPartStatus = ((Label) row.Cells[ViewState("StatusIndex")].FindControl("lblPartStatus"));