DataGridTextBoxColumn.Paint Method (Graphics, Rectangle, CurrencyManager, Int32, Brush, Brush, Boolean)

Use BaseTrue

Paints a DataGridColumnStyle with the specified Graphics, Rectangle, CurrencyManager, row number, Brush, and foreground color.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

protected internal override void Paint(
	Graphics g,
	Rectangle bounds,
	CurrencyManager source,
	int rowNum,
	Brush backBrush,
	Brush foreBrush,
	bool alignToRight


Type: System.Drawing.Graphics

The Graphics object to draw to.

Type: System.Drawing.Rectangle

The bounding Rectangle to paint into.

Type: System.Windows.Forms.CurrencyManager

The CurrencyManager of the DataGrid the that contains the column.

Type: System.Int32

The number of the row in the underlying data table.

Type: System.Drawing.Brush

A Brush that paints the background.

Type: System.Drawing.Brush

A Brush that paints the foreground color.

Type: System.Boolean

A value indicating whether to align the column's content to the right. true if the content should be aligned to the right; otherwise, false.

The Paint method uses the GetColumnValueAtRow to determine the value to draw in the cell. The PaintText method is called to draw the cell and its contents.

The following example uses the Paint method to paint a clicked cell with new foreground and background color.

public class Form1: Form
 protected DataGrid dataGrid1;
 protected DataSet myDataSet;

private void PaintCell(object sender, MouseEventArgs e)
    // Use the HitTest method to get a HitTestInfo object.
    DataGrid.HitTestInfo hi;
    DataGrid grid = (DataGrid)sender;
    hi=grid.HitTest(e.X, e.Y);
    // Test if the clicked area was a cell.
    if(hi.Type == DataGrid.HitTestType.Cell)
       // If it's a cell, get the GridTable and ListManager of the
       // clicked table.         
       DataGridTableStyle dgt = dataGrid1.TableStyles[0];
       CurrencyManager cm = (CurrencyManager)this.BindingContext[myDataSet.Tables[dgt.MappingName]];
       // Get the Rectangle of the clicked cell.
       Rectangle cellRect;
       cellRect=grid.GetCellBounds(hi.Row, hi.Column);
       // Get the clicked DataGridTextBoxColumn.
       MyGridColumn  gridCol =(MyGridColumn)dgt.GridColumnStyles[hi.Column];
       // Get the Graphics object for the form.
       Graphics g = dataGrid1.CreateGraphics();
       // Create two new Brush objects, a fore brush, and back brush.
       Brush fBrush = new System.Drawing.SolidBrush(Color.Blue);
       Brush bBrush= new System.Drawing.SolidBrush(Color.Yellow);
       // Invoke the Paint method to paint the cell with the brushes.
       gridCol.PaintCol(g, cellRect, cm, hi.Row, bBrush, fBrush, false);

public class MyGridColumn:DataGridTextBoxColumn{
   public void PaintCol(Graphics g, Rectangle cellRect, 
   	CurrencyManager cm, int rowNum, Brush bBrush, 
   	Brush fBrush, bool isVisible){
      this.Paint(g, cellRect, cm, rowNum, bBrush, fBrush, isVisible);

.NET Framework
Available since 1.1
Return to top