//overridden to fire BoolChange event and Formatting event protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowIndex, Brush backBrush, Brush foreBrush, bool alignToRight) { var colNum = DataGridTableStyle.GridColumnStyles.IndexOf(this); //used to handle the boolchanging ManageBoolValueChanging(rowIndex, colNum); //fire formatting event DataGridFormatCellEventArgs e = null; var callBaseClass = true; if (SetCellFormat != null) { e = new DataGridFormatCellEventArgs(rowIndex, colNum, GetColumnValueAtRow(source, rowIndex)); SetCellFormat(this, e); if (e.BackBrush != null) { backBrush = e.BackBrush; } callBaseClass = e.UseBaseClassDrawing; } if (callBaseClass) { base.Paint(g, bounds, source, rowIndex, backBrush, new SolidBrush(Color.Red), alignToRight); } //clean up if (e != null) { if (e.BackBrushDispose) { e.BackBrush.Dispose(); } if (e.ForeBrushDispose) { e.ForeBrush.Dispose(); } if (e.TextFontDispose) { e.TextFont.Dispose(); } } }
/// <summary> /// used to fire an event to retrieve formatting info /// and then draw the cell with this formatting info /// </summary> /// <param name="g"></param> /// <param name="bounds"></param> /// <param name="source"></param> /// <param name="rowIndex"></param> /// <param name="backBrush"></param> /// <param name="foreBrush"></param> /// <param name="alignToRight"></param> protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowIndex, Brush backBrush, Brush foreBrush, bool alignToRight) { DataGridFormatCellEventArgs e = null; var callBaseClass = true; //fire the formatting event if (SetCellFormat != null) { var col = DataGridTableStyle.GridColumnStyles.IndexOf(this); e = new DataGridFormatCellEventArgs(rowIndex, col, GetColumnValueAtRow(source, rowIndex)); SetCellFormat(this, e); if (e.BackBrush != null) { backBrush = e.BackBrush; } //if these properties set, then must call drawstring if (e.ForeBrush != null || e.TextFont != null) { if (e.ForeBrush == null) { e.ForeBrush = foreBrush; } if (e.TextFont == null) { e.TextFont = DataGridTableStyle.DataGrid.Font; } g.FillRectangle(backBrush, bounds); var saveRegion = g.Clip; var rect = new Rectangle(bounds.X, bounds.Y, bounds.Width, bounds.Height); using (var newRegion = new Region(rect)) { g.Clip = newRegion; var charWidth = (int)Math.Ceiling(g.MeasureString("c", e.TextFont, 20, StringFormat.GenericTypographic).Width); var s = GetColumnValueAtRow(source, rowIndex).ToString(); var maxChars = Math.Min(s.Length, (bounds.Width / charWidth)); try { g.DrawString(s.Substring(0, maxChars), e.TextFont, e.ForeBrush, bounds.X, bounds.Y + 2); } catch (Exception ex) { Console.WriteLine(ex.Message.ToString()); } //empty catch finally { g.Clip = saveRegion; } } callBaseClass = false; } if (!e.UseBaseClassDrawing) { callBaseClass = false; } } if (callBaseClass) { base.Paint(g, bounds, source, rowIndex, backBrush, foreBrush, alignToRight); } //clean up if (e != null) { if (e.BackBrushDispose) { e.BackBrush.Dispose(); } if (e.ForeBrushDispose) { e.ForeBrush.Dispose(); } if (e.TextFontDispose) { e.TextFont.Dispose(); } } }