//overridden to fire BoolChange event and Formatting event protected override void Paint(System.Drawing.Graphics g, System.Drawing.Rectangle bounds, System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Brush backBrush, System.Drawing.Brush foreBrush, bool alignToRight) { int colNum = this.DataGridTableStyle.GridColumnStyles.IndexOf(this); //used to handle the boolchanging ManageBoolValueChanging(rowNum, colNum); //fire formatting event DataGridFormatCellEventArgs e = null; bool callBaseClass = true; if (SetCellFormat != null) { e = new DataGridFormatCellEventArgs(rowNum, colNum, this.GetColumnValueAtRow(source, rowNum)); SetCellFormat(this, e); if (e.BackBrush != null) { backBrush = e.BackBrush; } callBaseClass = e.UseBaseClassDrawing; } if (callBaseClass) { base.Paint(g, bounds, source, rowNum, 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(); } } }
//used to fire an event to retrieve formatting info //and then draw the cell with this formatting info protected override void Paint(System.Drawing.Graphics g, System.Drawing.Rectangle bounds, System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Brush backBrush, System.Drawing.Brush foreBrush, bool alignToRight) { DataGridFormatCellEventArgs e = null; bool callBaseClass = true; //fire the formatting event if (SetCellFormat != null) { int col = this.DataGridTableStyle.GridColumnStyles.IndexOf(this); e = new DataGridFormatCellEventArgs(rowNum, col, this.GetColumnValueAtRow(source, rowNum)); 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 = this.DataGridTableStyle.DataGrid.Font; } g.FillRectangle(backBrush, bounds); Region saveRegion = g.Clip; Rectangle rect = new Rectangle(bounds.X, bounds.Y, bounds.Width, bounds.Height); using (Region newRegion = new Region(rect)) { g.Clip = newRegion; int charWidth = (int)Math.Ceiling(g.MeasureString("c", e.TextFont, 20, StringFormat.GenericTypographic).Width); string s = this.GetColumnValueAtRow(source, rowNum).ToString(); int 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, rowNum, 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(); } } }