簡體   English   中英

DevExpress Gridview - 基於特定條件在 C# 中格式化行

[英]DevExpress Gridview - format rows in C# based on certain condition

我正在使用 DevExpress GridView 並通過 C# 代碼綁定數據。

我的要求是根據我在代碼邏輯中執行的計算更改行顏色。

我想遍歷所有可用的行,如果行單元格值與條件的結果匹配,我需要更改顏色。

我在網上看到過示例和示例源,但似乎沒有任何內容可以讓我解決這個問題。 他們中的大多數都在使用 XAML 代碼或使用 DataBinding 或在 DataBound 等期間。我不太習慣使用 inotifyvaluechange。

期待您的支持和建議。

為 DevExpress Gridview 試試這個

using DevExpress.XtraGrid.Views.Grid;

private void gridView1_RowStyle(object sender, 
DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e) {
   GridView View = sender as GridView;
   if(e.RowHandle >= 0) {
      string category = View.GetRowCellDisplayText(e.RowHandle, View.Columns["Category"]);
      if(category == "Beverages") {
         e.Appearance.BackColor = Color.Salmon;
         e.Appearance.BackColor2 = Color.SeaShell;
      }            
   }
}

輸出

在此處輸入圖片說明

private void grvAssesse_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
{
  if (e.RowHandle >= 0)
  {
    GridView View = sender as GridView;        
    if (Condition)
    {
      e.Appearance.ForeColor = Color.Red;
    }
  }
}

您需要全局設置條件,基於該條件網格將為每行加載執行此事件。

更多詳情請參考: GridView

您可以處理 HtmlRowPrepared 事件以更改行的顏色。

以下是可以幫助您的示例:

   protected void ASPxGridView1_HtmlRowPrepared(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewTableRowEventArgs e) {
        if (e.RowType != GridViewRowType.Data) return;                    
            e.Row.BackColor = System.Drawing.Color.LightCyan; // Changes The BackColor of ENTIRE ROW
            e.Row.ForeColor = System.Drawing.Color.DarkRed; // Change the Font Color of ENTIRE ROW
    }

您還可以設置一些條件,以便只有幾行會被着色,例如:

 protected void ASPxGridView1_HtmlRowPrepared(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewTableRowEventArgs e) {

    if (e.RowType != GridViewRowType.Data) return;       
     int VALUE = Convert.ToInt32(e.GetValue("COLUMNNAME"));
          if (VALUE < 20)          
       {
                e.Row.BackColor = System.Drawing.Color.LightCyan; // Changes The BackColor of ENTIRE ROW
                e.Row.ForeColor = System.Drawing.Color.DarkRed; // Change the Font Color of ENTIRE ROW
        }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM