我想根据绑定对象的属性为特定行添加背景颜色.
我有(并且它有效)的解决方案是使用事件,DataBindingComplete
但我不认为这是最好的解决方案.
这是事件:
private void myGrid_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) { for (int i = 0; i < this.myGrid.Rows.Count; i++) { if((this.myGrid.Rows[i].DataBoundItem as MyObject).Special) { this.myGrid.Rows[i].DefaultCellStyle.BackColor = Color.FromArgb(240, 128, 128); } } }
还有其他更好的选择吗?
您还可以将事件处理程序附加到RowPostPaint:
dataGridView1.RowPostPaint += OnRowPostPaint; void OnRowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) { MyObject value = (MyObject) dataGridView1.Rows[e.RowIndex].DataBoundItem; DataGridViewCellStyle style = dataGridView1.Rows[e.RowIndex].DefaultCellStyle; // Do whatever you want with style and value .... }