2011-11-09 47 views
2

我正面臨着datagridview的一個奇怪問題。 我需要改變所選單元格(A)的樣式以響應另一個單元格(B)= x的值。 (A)是一個文本框,而(B)是一個組合框。 我發現事件CellEndEdit,當用戶更改(B)的值時,一切正常:(A)的樣式立即改變。Datagridview單元格樣式更新

現在,當我嘗試以編程方式更新datagridview時,這不起作用。 奇怪的是,兩種方法共享同一個方法UpdateTimeChannelCell。 如果我以編程方式調用此方法,則datagridview不會更新其單元格的樣式。我試圖通過更新,令人耳目一新,在DataGridView沒有運氣

 private void UpdateTimeChannelCell(DataGridViewCellEventArgs e) 
    { 
     if (e.ColumnIndex == 1 || e.ColumnIndex == 3 || e.ColumnIndex == 5 || e.ColumnIndex == 7 || e.ColumnIndex == 9 || e.ColumnIndex == 11 || e.ColumnIndex == 13) 
     { 
      if ((int)this.dataGridView_TidKanaler.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == 0) 
      { 
       this.dataGridView_TidKanaler.Rows[e.RowIndex].Cells[e.ColumnIndex - 1].Value = new Time(); 
       this.dataGridView_TidKanaler.Rows[e.RowIndex].Cells[e.ColumnIndex - 1].Style = disableStyle; 
      } 
      else 
      { 
       this.dataGridView_TidKanaler.Rows[e.RowIndex].Cells[e.ColumnIndex - 1].Style = enableStyle; 
      } 
     } 
    } 
+0

可以顯示disableStyle和enableStyle代碼 – Sandy

+0

this.enableStyle = new DataGridViewCellStyle(){ForeColor = Color.Black,SelectionBackColor = Color.LightSteelBlue}; this.disableStyle = new DataGridViewCellStyle(){ForeColor = Color.White,SelectionBackColor = Color.White}; – Francesco

回答

3

據我所知,所有的造型爲DataGridViews需要在DataGridView.CellFormatting事件中發生失效。

此事件是您改變單元格的默認樣式/着色的機會。

+0

你能舉一個例子說明事件如何用來捕捉單元格中的值的變化嗎? – Francesco

+0

與單元格格式化事件一樣,您不捕獲任何一個更改,每次更改DataGridView時都會觸發每個單元格,因此您會根據當前狀態檢查單元格的樣式。 http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellformatting%28v=vs.85%29.aspx – Gent

+0

@Gent this statement「DataGridViews的樣式需要發生在DataGridView.CellFormatting「在我的一個表單中是不正確的,我更新了單元格值並同時設置了樣式。 – Ken

0

我必須強制刷新我的網格才能更新樣式。 例如。 this.dataGridView_TidKanaler.Refresh()

相關問題