/// <summary> /// cell font color /// </ summary> Color cellForeColor = Color.Black; /// <summary> /// cell background normal color /// </ summary> Color cellNomalColor = Color.white; /// <summary> /// error color de fondo de células /// </ resumen> color = cellErrorColor Color.Red; /// <summary> /// células edición /// </ resumen> /// <param name = "remitente"> </ param> /// <param name = "e "> </param> privado void dgvSnpDistribute_EditingControlShowing ( remitente de objeto , DataGridViewEditingControlShowingEventArgs e) { ComboBox cb = e.Control como ComboBox; // if (cbox! = null &&! object.ReferenceEquals (cbox, cb) && cb! = null || cbox == null) // { // cbox = cb; // } if (cb! = null ) { cb.DropDownStyle = ComboBoxStyle.DropDownList; cb.DrawMode = DrawMode.OwnerDrawFixed; cb.DrawItem - =Cb_DrawItem; cb.DrawItem + = Cb_DrawItem; } } /// <summary> /// 单元 格 重绘 /// </summary> /// <param name = "sender"> </param> /// <param name = "e"> </ param> private void Cb_DrawItem ( remitente del objeto , DrawItemEventArgs e) { if (e.Index < 0 ) { return ; } ComboBox cb = remitente como ComboBox; si (cb! ) { cb.BackColor = cellNomalColor; valor de cadena = 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> /// <param name = "sender"> </param> /// <param name = "e" > </param> private void dgvSnpDistribute_CellPainting ( remitente de objeto , DataGridViewCellPaintingEventArgs e) { if (e.ColumnIndex < 0 || e.RowIndex < 0 ) { return ; = dgvSnpDistribute.Rows [e.RowIndex] .Cells [e.ColumnIndex] como DataGridViewComboBoxCell; if (cell! = null ) { e.Paint (e.ClipBounds, DataGridViewPaintParts.Border); e.Paint (e.ClipBounds, DataGridViewPaintParts.ContentBackground); usando (Brush foreBrush = new SolidBrush (cellForeColor)) usando (Brush backBrush = new SolidBrush (cell.Style.BackColor)) usando (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 = verdadero ; } }
En la condición incorrecta, cambie el color de fondo de cell.Style.BackColor