2012-03-02 59 views
11

我想在加載時甚至在Windows窗體中根據特定條件更改DGV行的背景顏色。但是我看不到任何DGV行的顏色變化。任何人都可以告訴我如何解決這個問題?DataGridView行的背景顏色不變

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); 
      } 
     } 
+1

爲什麼不設置'BackColor',而不是'ForeColor' – V4Vendetta 2012-03-02 04:31:53

+0

我已經試過這爲好,但我可以」 t請參閱DGV的任何行中行顏色的任何更改 – Stardust 2012-03-02 04:35:30

回答

11

我認爲最好的地方是設置背景色的CellFormatting事件DataGridView的,一些在這些線路上。

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; 
} 
+0

謝謝!它已經工作了! – Stardust 2012-03-02 04:46:02

+4

如King_Rob所述,Cellformatting即使在滾動時也會持續觸發事件。謹慎使用它。 – henry 2014-05-20 03:30:35

+0

該事件發生多次。 – 2015-06-13 13:55:40

9

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

問題的最簡單的解決辦法可能是將代碼填入/表格的Shown()方法中,而不是構造函數。下面是msdn論壇中發佈的一篇文章的鏈接,它將我引入解決方案,它被標記爲答案約3/4。

MSDN forums post with the Solution

+0

這應該是正確的答案。只需添加實現細節:使用Load事件設置標誌,然後使用Visible事件設置背景顏色。 – Daltons 2016-05-12 11:02:38

+0

這是正確的答案。提高性能,因爲它不會像使用cellformatting和其他處理程序那樣多次觸發 – Anand 2016-11-16 09:33:30

0

對不起,我遲到的答案,但我只是正好面對現在同樣的問題。

我有事情,不要在構造函數中正常工作,一些通用的解決方案 - 使用定時器

設置它像100毫秒一些很短的時間。然後在構造函數中,你將有

timer1.Enabled=true 

和timer_Tick事件:

timer1.Enabled=false 

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

,每天一次爲我工作。

3

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 
    } 
} 
+0

最簡單的答案!謝謝! – 2016-08-15 15:08:07

-1

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

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

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

`

+0

這甚至可以幫助用戶嗎? – gmetax 2017-04-28 13:21:37

+0

也許它與其他Microsoft產品相同,並且關閉並重新啓動('Visible = false; Visible = true')確實有幫助嗎? – derM 2017-04-28 13:59:31

+0

如果它有效(它適用於我),這意味着它有幫助。你是否同意我的觀點? – SCG 2017-04-28 15:26:05