2010-07-05 94 views

回答

1

趁着DataGridViewCell的事件CellEnterCellLeave你可以嘗試像這個:

private void foobarDataGridView_CellEnter(object sender, DataGridViewCellEventArgs e) 
{ 
    DataGridViewCellStyle fooCellStyle = new DataGridViewCellStyle(); 
    fooCellStyle.BackColor = System.Drawing.Color.LightYellow; 
    this.VariableFinderDataGridView.CurrentCell.Style.ApplyStyle(fooCellStyle); 
} 

private void foobarFinderDataGridView_CellLeave(object sender, DataGridViewCellEventArgs e) 
{ 
    DataGridViewCellStyle barCellStyle = new DataGridViewCellStyle(); 
    barCellStyle.BackColor = System.Drawing.Color.White; 
    this.VariableFinderDataGridView.CurrentCell.Style.ApplyStyle(barCellStyle); 
} 
+0

如果選擇前的行顏色不是白色,該怎麼辦? – Thunder 2012-05-25 11:29:32

+0

我的意思是這只是一個快速示例來說明這個概念 - 您只需創建一個輔助方法來檢索您需要切換的顏色。 – 2012-05-26 17:48:36

26

來吧夥計...必須有一個簡單的解決方案,並最終得到一個。

dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Blue; 
dataGridView1.DefaultCellStyle.SelectionForeColor = Color.Red; 

這適用於我,沒有複雜的代碼,沒有事件處理。我沒有過,但沒能想起這麼認爲發佈它會幫助別人,我在未來:)

+1

完美答案!!!謝謝。 – SKR 2017-10-26 10:39:57

0

這裏是我的代碼

private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e) 
{ 
dataGridView1.CurrentRow.DefaultCellStyle.BackColor = Color.Maroon; 
dataGridView1.CurrentRow.DefaultCellStyle.ForeColor = Color.White; 
} 
+0

不錯的一個亞當!我從來不知道存在! – 2016-12-02 00:45:37