簡體   English   中英

DataGridView 行的背景顏色沒有改變

[英]DataGridView row's background color is not changing

即使在 Windows 窗體中,我也想根據加載時的特定條件更改 DGV 行的背景顏色。 但是我看不到任何 DGV 的行有任何顏色變化。 誰能告訴我如何解決這個問題?

private void frmSecondaryPumps_Load(object sender, EventArgs e)
{
            try
            {
                DataTable dt = DeviceData.BindData("SECONDARY_PUMPS".ToUpper());
                dataGridView1.DataSource = dt;

                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    foreach (DataGridViewColumn column in dataGridView1.Columns)
                    {
                        if (row.Cells[column.Name] != null)
                        {
                            if (row.Cells[column.Name].Value.ToString() == "ON")
                                row.DefaultCellStyle.BackColor = System.Drawing.Color.Green;

                            if (row.Cells[column.Name].Value.ToString() == "OFF")
                                row.DefaultCellStyle.BackColor = System.Drawing.Color.Red;
                        }
                    }
                }

                dataGridView1.Refresh();
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }

使用cellformattingdatabindingcomplete甚至paint事件的問題之一是它們被多次觸發。 從我收集到的信息來看, datagridview控件存在一個問題,即在顯示表單之前您無法更改任何單元格的顏色。 因此,在調用Shown()之前運行的方法或觸發的事件不會改變顏色。 定位為問題解決方案的事件通常有效,但由於它們被多次調用,可能不是最有效的答案。

這個問題最簡單的解決方案可能是在表單的Shown()方法而不是構造函數中放置用於填充/着色網格的代碼。 下面是 msdn 論壇中向我提示解決方案的帖子的鏈接,它被標記為頁面下方約 3/4 處的答案。

MSDN 論壇發布了解決方案

我認為最好的地方是在DataGridViewCellFormatting事件中設置 BackColor,在這些行上。

private void grid1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    DataGridViewRow row = grid1.Rows[e.RowIndex];// get you required index
    // check the cell value under your specific column and then you can toggle your colors
    row.DefaultCellStyle.BackColor = Color.Green;
}

King_Rob 是正確的。 我有同樣的問題,所以我只會發布我的實現,因為這里的其他建議遠非最佳。

添加事件處理程序(在設計器或構造器中):

this.Load += UserControl_Load; // or form or any control that is parent of the datagridview
dataGridView1.VisibleChanged += DataGridView1_VisibleChanged;

在加載事件處理程序方法中添加一個標志

private bool _firstLoaded;
private void UserControl_Load(object sender, EventArgs e)
{
    _firstLoaded = true;
}

最后在可見事件處理程序方法中:

private void DataGridView1_VisibleChanged(object sender, EventArgs e)
{
    if (_firstLoaded && dataGridView1.Visible)
    {
        _firstLoaded = false;
        // your code
    }
}

抱歉回復晚了,但我現在正面臨完全相同的問題。

對於在構造函數中無法正常工作的事情,我有一些通用的解決方案 - 使用計時器

將其設置為 100 毫秒等短時間。 然后在構造函數中,您將擁有

timer1.Enabled=true

在 timer_Tick 事件中:

timer1.Enabled=false

and all the code that doesn't work in constructor goes here...

它每次都對我有用。

主要問題是您何時運行此代碼。 您使用觸發器_Load運行它,這就是它不着色的原因,當您使用_Shown上的觸發器_Shown運行相同的代碼時,着色將起作用。 當加載表單時會觸發加載,因此在您的代碼之后仍然可能有一些幕后代碼在運行。 顯示的觸發器在所有操作完成且表單完全加載后運行。

使用以下代碼更改 Datagrid 項目顏色

Datagrid1.SelectedIndex = e.Item.ItemIndex;
Datagrid1.Items[e.Item.ItemIndex].Cells[0].BackColor = System.Drawing.Color.Green;

這段代碼快速,簡單,不消耗內存!

例如,在 CellEndEdit 事件中使用此代碼

 `try{
 //your code
 }
 catch(Exception){
 //your exception
 }
finally{
yourDataGridView.Visible = false;
 yourDataGridView.Visible = true;
}

`

暫無
暫無

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

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