2011-04-20 164 views
1

我已經在DataGridView上添加了CellFormatting事件的處理程序,以根據行的內容修改背景顏色。DataGridView CellFormatting事件不會觸發

即使數據被插入表中,它似乎也不會被觸發。我通過在CellFormatting事件的IDE中雙擊來添加事件處理程序,這似乎正確地創建了代碼。

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
    { 
     // this never gets called 
     MessageBox.Show("Event fired"); 
    } 

我能做什麼錯?

回答

2

我認爲你不能爲你的情況使用CellFormating事件。當單元格的內容需要格式化顯示時纔會發生。

嘗試CellValueChanged事件,而不是(http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellvaluechanged.aspx)

或者

選擇其他合適的事件從http://msdn.microsoft.com/en-us/library/x4dwfh7x.aspx

+0

我結束了使用RowsAdded事件捕獲我所需要的。謝謝! – Dave 2011-04-21 02:39:19

2

你可以嘗試RowValidated事件:

private void dataGridView1_RowValidated(object sender, DataGridViewCellEventArgs e) 
{ 
     dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Blue; 
} 

注意:當你點擊行,當你關閉窗體此事件將觸發。