开发者

ASP.NET GridView use FindControl() on BoundField to manipulate field

I'm working with an old app that had hard coded columns for different locations, now that new locations are being added I decided to try and make things populate dynamically. One of the features of the app was displaying red text and bolding text when a status was deemed "bad". This was executed by using the "FindControl()" function from the cells within the selected row with TemplateFields.

Now that I've set this up to use a bound field, how would I go about changing the text color, size, etc. during the DataBound event?

BoundField Being Added To GridView

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

DataBound Event For GridView

    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"));
        开发者_JS百科    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;
            }
        }
    }


Years ago I wrote some code to help you find a cell index using the column's SortExpression, HeaderText or DataField. It's saved me lots of effort over the years, and you just call it like 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);
    }
}

Once you have the cell, I suggest you manipulate it by setting Cell.CssClass, and then using CSS to style it accordingly. Steer clear of inline style, which is what you get when you set ForeColor, BackColor etc.


If you always have X columns in your grid, you can access them like that:

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

  }

now change cell index to whatever index column you are intrested in has... attach the function to OnRowDataBound event instead of OnDataBound


A crude solution would be to add the column index into ViewState ...

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

... and then use it again on databound

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜