DataGridViewドロップダウンボックスのセルが間違っている場合、プロンプトとして背景色を変更する

        ///  <summary> 
        /// セルのフォントの色
         ///  </ summary> 
        Color cellForeColor = Color.Black;
         ///  <summary> 
        /// セルの背景の通常の色
         ///  </ summary> 
        色cellNomalColor = Color.White;
         ///  <まとめ> 
        /// セルの背景色誤差
         ///  </要約> 
        色= cellErrorColor Color.Red;
         ///  <まとめ> 
        /// 編集細胞
         ///  </概要> 
        ///  <param name = "sender"> </ param> 
        ///  <param name = "e "> </ param> 
        プライベート void dgvSnpDistribute_EditingControlShowing(object sender、DataGridViewEditingControlShowingEventArgs e)
        { 
            ComboBox cb = e.Control as 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(object sender、DrawItemEventArgs e)
        { 
            if(e.Index < 0 
            { 
                return ; 
            } 

            コンボボックスCB =送信者としてコンボボックス;
            if(cb!= null 
            { 
                cb.BackColor = cellNomalColor;
                文字列値= 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(object sender、DataGridViewCellPaintingEventArgs e)
        { 
            if(e.ColumnIndex < 0 || e.RowIndex < 0 
            { 
                リターン; 
            } 
            DataGridViewComboBoxCell cell
            { 
                e.Paint(e.ClipBounds、DataGridViewPaintParts.Border);= dgvSnpDistribute.Rows [e.RowIndex] .Cells [e.ColumnIndex] as DataGridViewComboBoxCell;
            if(cell!= null
                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 ; 
            } 
        }

間違った状態で、cell.Style.BackColorの背景色を変更します

おすすめ

転載: www.cnblogs.com/pilgrim/p/12171309.html