如何在C#窗口應用程序中更改datagridview選定的行背景顏色?如何更改datagridview選定的行背景顏色?
回答
在DataGridView上有一個DefaultCellStyle
,裏面有SelectionBackColor
和SelectionForeColor
屬性。
DataGridView中使用樣式繼承的想法,如果你發現沒有施加你挑款式:
趁着DataGridViewCell
的事件CellEnter
和CellLeave
你可以嘗試像這個:
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);
}
如果選擇前的行顏色不是白色,該怎麼辦? – Thunder 2012-05-25 11:29:32
我的意思是這只是一個快速示例來說明這個概念 - 您只需創建一個輔助方法來檢索您需要切換的顏色。 – 2012-05-26 17:48:36
來吧夥計...必須有一個簡單的解決方案,並最終得到一個。
dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Blue;
dataGridView1.DefaultCellStyle.SelectionForeColor = Color.Red;
這適用於我,沒有複雜的代碼,沒有事件處理。我沒有過,但沒能想起這麼認爲發佈它會幫助別人,我在未來:)
完美答案!!!謝謝。 – SKR 2017-10-26 10:39:57
這裏是我的代碼
private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
{
dataGridView1.CurrentRow.DefaultCellStyle.BackColor = Color.Maroon;
dataGridView1.CurrentRow.DefaultCellStyle.ForeColor = Color.White;
}
不錯的一個亞當!我從來不知道存在! – 2016-12-02 00:45:37
- 1. 如何更改wpf datagridview中行的背景顏色?
- 2. C#更改背景顏色特定行
- 3. 我如何更改jtabbedpane選定的選項卡背景顏色
- 4. 在mouseover上更改datagridview中選定行的背景色
- 5. 如何更改我的圖像選定的背景顏色?
- 6. 更改ListView的背景選擇顏色?
- 7. 更改所選JToggleButton的背景顏色
- 8. 如何更改gridview選定的項目背景顏色?
- 9. 如何更改Windows Phone中選定項目的背景顏色?
- 10. 如何更改Radscheduler的背景顏色
- 11. 如何更改JLabel的背景顏色?
- 12. 如何更改SFSafariViewController的背景顏色?
- 13. 如何更改fusionchart的背景顏色?
- 14. 如何更改editText的背景顏色?
- 15. 如何更改JFrame的背景顏色
- 16. 如何更改ggvis的背景顏色?
- 17. 如何更改tr的背景顏色
- 18. 如何更改gnuplot的背景顏色?
- 19. 如何更改apDiv的背景顏色?
- 20. 如何更改JOptionPane的背景顏色?
- 21. 如何更改PopupMenu背景的顏色
- 22. 如何更改ListViewItem的背景顏色?
- 23. 如何更改gtk.MenuItem()的背景顏色
- 24. 更改行的背景顏色extjs4
- 25. DataGridView行的背景顏色不變
- 26. 更改UITableViewCell背景顏色時選擇
- 27. 更改背景顏色選擇
- 28. 如何更改RichTextBox中特定行的背景顏色?
- 29. 如何從黑色背景更改背景顏色?
- 30. 更改背景顏色
你需要更多的細節添加到這個問題,也許張貼一些代碼和你已經嘗試過什麼,失敗了。你的問題現在不能回答。 – Oded 2010-07-05 09:32:57