首页 >> 新车 >

人日羊b日得进去

2024-07-08 02:43:30 来源: 用户: 

要求是选中的行变色,代码如下事件是RowPrePaint。

这个思路是先得到当前的行。

RowPrePaint事件应该是每Paint一行之前的事件,所以对行进行检查,如果满足要求就设置成想要的样式即可。

因为要把非当前行还原样式,所以记录了之前的颜色,估计直接记录Style也是一种好方法。

1. void DataGridView1RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)2. {3. if (e.RowIndex >= dataGridView1.Rows.Count - 1)4. return;5. var row = (sender as DataGridView).Rows[e.RowIndex];6. try7. {8. Color oldForeColor = new Color();9. Color oldBackColor = new Color();10. if (row == (sender as DataGridView).CurrentRow) {11. if(row.DefaultCellStyle.ForeColor != Color.White)12. {13. oldForeColor = row.DefaultCellStyle.ForeColor;14. row.DefaultCellStyle.ForeColor = Color.White;15. }16. if(row.DefaultCellStyle.BackColor != ***.Blue)17. {18. oldBackColor = row.DefaultCellStyle.BackColor;19. row.DefaultCellStyle.BackColor = ***.Blue;20. }21. }22. else23. {24. row.DefaultCellStyle.ForeColor = oldForeColor;25. row.DefaultCellStyle.BackColor = oldBackColor;26. }27. }28. catch (Exception)29. {30. }31. }。

  免责声明:本文由用户上传,与本网站立场无关。财经信息仅供读者参考,并不构成投资建议。投资者据此操作,风险自担。 如有侵权请联系删除!

 
分享:
最新文章