首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計疑難解答

.NET GridView 庫存數量動態顏色:小於 10 紅色,否則綠色

     編輯:程式設計疑難解答

實現

在 `RowDataBound` 事件中判斷並設置單元格顏色:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {
    if (e.Row.RowType == DataControlRowType.DataRow) {
        int stock = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "StockCount"));
        if (stock < 10) {
            e.Row.Cells[庫存列索引].ForeColor = System.Drawing.Color.Red;
        } else {
            e.Row.Cells[庫存列索引].ForeColor = System.Drawing.Color.Green;
        }
    }
}

或使用模板列 `` 在 `Eval` 中綁定樣式類別。

參考

CSDN 相關討論

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved