5

我正在使用一个旧应用程序,该应用程序具有针对不同位置的硬编码列,现在正在添加新位置,我决定尝试动态填充内容。该应用程序的功能之一是在状态被视为“不良”时显示红色文本和粗体文本。这是通过使用带有 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;
            }
        }
    }
4

3 回答 3

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等等。

于 2011-07-21T08:50:17.700 回答
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

于 2011-07-21T08:36:05.457 回答
-2

一个粗略的解决方案是将列索引添加到 ViewState ...

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

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

Label lblPartStatus = ((Label) row.Cells[ViewState("StatusIndex")].FindControl("lblPartStatus"));
于 2011-07-21T08:41:16.853 回答