我真的很想更改单个 DataGridVeiwComboBox 控件的选定项的样式。
有一个数据库将信息输入我的 DataGridView,如果由于某种原因数据无效,我想以某种方式通知用户。我决定将下拉文本字体设为红色,背景设为蓝色,这样它就可以在数百行的列表中脱颖而出。
当存在无效数据(意味着来自数据库的数据不是下拉列表中的选项之一)时,调用 DataError 回调:
private void OnDataError(object sender, DataGridViewDataErrorEventArgs e)
{
DataGridViewCellStyle style = new DataGridViewCellStyle();
style.ForeColor = Color.Red;
style.BackColor = Color.Blue;
((DataGridViewComboBoxCell)(((DataGridView)(sender))[e.ColumnIndex, e.RowIndex])).Style = style;
e.Cancel = true;
}
但是,这会导致以下结果:
我不希望下拉更改,我希望更改所选值的“0”。
是否有另一种方法来通知用户该值无效?我不想创建一个列表并将其呈现给用户,行数可能非常大。