Winform中DataGridView下拉框单元格错误时,改变背景色,作为提示
在错误的条件时,更改cell.Style.BackColor的背景颜色
/// <summary> /// 单元格字体颜色 /// </summary> Color cellForeColor = Color.Black; /// <summary> /// 单元格背景正常颜色 /// </summary> Color cellNomalColor = Color.White; /// <summary> /// 单元格背景错误颜色 /// </summary> Color cellErrorColor = Color.Red; /// <summary> /// 编辑单元格 /// </summary> private void dgvGroupConfig_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { isDrawToString = false; if (e.Control is DataGridViewComboBoxEditingControl cb) { cb.DropDownStyle = ComboBoxStyle.DropDownList; cb.DrawMode = DrawMode.OwnerDrawFixed; cb.DrawItem -= Cb_DrawItem; cb.DrawItem += Cb_DrawItem; } var dgv = sender as DataGridView; if (dgv.CurrentCell.ColumnIndex == cbxDriller.Index || dgv.CurrentCell.ColumnIndex == cbxSupervisor.Index) { isDrawToString = true; } } bool isDrawToString = false; /// <summary> /// 单元格重绘 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Cb_DrawItem(object sender, DrawItemEventArgs e) { if (e.Index < 0) { return; } if (sender is DataGridViewComboBoxEditingControl cb) { cb.BackColor = cellNomalColor; string value = cb.GetItemText(cb.Items[e.Index]); if (isDrawToString) value = cb.Items[e.Index].ToString(); using (Brush foreBrush = new SolidBrush(cellForeColor)) using (Brush backBrush = new SolidBrush(cb.BackColor)) { e.Graphics.FillRectangle(backBrush, e.Bounds); e.DrawBackground(); e.DrawFocusRectangle(); e.Graphics.DrawString(value, cb.Font, foreBrush, e.Bounds); } } } /// <summary> /// 重新编写单元格绘制函数 /// </summary> private void dgvGroupConfig_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { var dgv = sender as DataGridView; if (e.ColumnIndex < 0 || e.RowIndex < 0) { return; } if (dgv.Rows[e.RowIndex].Cells[e.ColumnIndex] is DataGridViewComboBoxCell cell) { e.Paint(e.ClipBounds, DataGridViewPaintParts.Border); e.Paint(e.ClipBounds, DataGridViewPaintParts.ContentBackground); using (Brush foreBrush = new SolidBrush(cellForeColor)) using (Brush backBrush = new SolidBrush(cell.Style.BackColor)) using (StringFormat format = new StringFormat()) { Rectangle rect = new Rectangle(e.CellBounds.X + 1, e.CellBounds.Y + 1, e.CellBounds.Width - 19, e.CellBounds.Height - 3); format.LineAlignment = StringAlignment.Center; e.Graphics.FillRectangle(backBrush, rect); e.Graphics.DrawString(cell.FormattedValue.ToString(), e.CellStyle.Font, foreBrush, rect, format); } e.Paint(e.ClipBounds, DataGridViewPaintParts.ErrorIcon); e.Paint(e.ClipBounds, DataGridViewPaintParts.Focus); e.Handled = true; } }
凡所有相,皆是虚妄。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?