/// <summary> /// Custom Paint Grid Cells /// </summary> private void PaintGridCell(PaintCell paint, object data, DataGridViewCellPaintingEventArgs e) { using (Brush gridBrush = new SolidBrush(dataGridView1.GridColor), backColorBrush = new SolidBrush(e.CellStyle.BackColor), selectionColorBrush = new SolidBrush(e.CellStyle.SelectionBackColor)) { using (var gridLinePen = new Pen(gridBrush)) { #region Erase (Set BackColor) the Cell and Choose Text Color Color textColor = Color.Black; if (paint.Equals(PaintCell.Status)) { e.Graphics.FillRectangle(backColorBrush, e.CellBounds); } else if (paint.Equals(PaintCell.Time) || paint.Equals(PaintCell.EtaDate)) { if (dataGridView1.Rows[e.RowIndex].Selected) { e.Graphics.FillRectangle(selectionColorBrush, e.CellBounds); textColor = Color.White; } else { e.Graphics.FillRectangle(backColorBrush, e.CellBounds); } } else if (paint.Equals(PaintCell.Warning)) { if (dataGridView1.Rows[e.RowIndex].Selected) { e.Graphics.FillRectangle(selectionColorBrush, e.CellBounds); textColor = Color.White; } else { e.Graphics.FillRectangle(Brushes.Orange, e.CellBounds); } } else { throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, "PaintCell Type '{0}' is not implemented", paint)); } #endregion #region Draw the grid lines if (Core.Application.IsRunningOnMono) { e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Top, e.CellBounds.Right, e.CellBounds.Top); e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right, e.CellBounds.Bottom - 1); } else { e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right, e.CellBounds.Bottom - 1); } #endregion #region Draw Cell Content (Text or Shapes) if (paint.Equals(PaintCell.Status)) { //// this is new... sometimes being passed a null //// value here. check and skip if so. - 1/28/12 //if (e.Value != null) //{ // Draw the inset highlight box. var newRect = new Rectangle(e.CellBounds.X + 4, e.CellBounds.Y + 4, e.CellBounds.Width - 10, e.CellBounds.Height - 10); var status = (SlotStatus)e.Value; e.Graphics.DrawRectangle(status.GetDrawingPen(), newRect); e.Graphics.FillRectangle(status.GetDrawingBrush(), newRect); //} //else //{ // Debug.WriteLine("Status value is null."); //} } else if (paint.Equals(PaintCell.Time)) { //if (e.Value != null) //{ PaintTimeBasedCellValue(textColor, e); //} } else if (paint.Equals(PaintCell.EtaDate)) { //if (e.Value != null) //{ Debug.Assert(data != null); DrawText(((DateTime)data).ToDateString(), textColor, e); //} } else if (paint.Equals(PaintCell.Warning)) { // Draw the text content of the cell //if (e.Value != null) //{ DrawText(e.Value.ToString(), textColor, e); //} } else { throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, "PaintCell Type '{0}' is not implemented", paint)); } #endregion e.Handled = true; } } }
/// <summary> /// Custom Paint Grid Cells /// </summary> private void PaintGridCell(PaintCell paint, DataGridViewCellPaintingEventArgs e) { PaintGridCell(paint, null, e); }