5

さまざまな場所の列がハードコーディングされていた古いアプリを使用していますが、新しい場所が追加されたので、物事を動的に入力することにしました。アプリの機能の 1 つは、ステータスが「悪い」と見なされたときに赤いテキストと太字のテキストを表示することでした。これは、TemplateFields で選択された行内のセルから「FindControl()」関数を使用して実行されました。

バインドされたフィールドを使用するようにこれを設定したので、DataBound イベント中にテキストの色やサイズなどを変更するにはどうすればよいでしょうか?

GridView に追加される BoundField

        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.CssClassCSSを使用してそれに応じてスタイルを設定することをお勧めします。ForeColor設定したときに得られるインラインスタイルなどを避けてくださいBackColor

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

  }

セル インデックスを、興味のあるインデックス列に変更します... 関数を OnDataBound の代わりに OnRowDataBound イベントにアタッチします

于 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 に答える