簡體   English   中英

根據單元格值更改DataGridView的行顏色

[英]Changing Row Color of DataGridView Based on Cell Value

我有兩列即文件名狀態的 Data Gridview DataGridview有這樣的數據源設置

 var bs = new BindingSourceAsync();
 bs.DataSource = data;
 dataGridView4.DataSource = bs;

使用async-await方法更新“狀態列”的值。當“狀態值”為“無效”時,如果其“有效”,我需要將相應的行顏色更改為紅色和綠色。

為此,我嘗試掛鈎到DataGridView的CellValueChanged事件

dataGridView4.CellValueChanged += DataGridView4_CellValueChanged;

但是該事件從未觸發過。我該如何解決此問題。請提出建議。

是的,在RowDataBound中獲取gridview

    protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        // check if it is the DataRow not the header row
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //Cell[1] is the cell contain the value for the condition
            if (Convert.ToInt16(e.Row.Cells[1].Text) < 10)
            {
                e.Row.Cells[0].BackColor = System.Drawing.Color.Yellow;
                e.Row.Cells[1].BackColor = System.Drawing.Color.Yellow;
            }
            else if (Convert.ToInt16(e.Row.Cells[1].Text) < 30)
            {
                e.Row.Cells[0].BackColor = System.Drawing.Color.Blue;
                e.Row.Cells[1].BackColor = System.Drawing.Color.Blue;
            }
        }
    }

暫無
暫無

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

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