Export (0) Print
Expand All

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

This member overrides DataGridColumnStyle.Paint(Graphics, Rectangle, CurrencyManager, Int32, Brush, Brush, Boolean), and more complete documentation might be available in that topic.

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
)

Parameters

g
Type: System.Drawing.Graphics

The Graphics object to draw to.

bounds
Type: System.Drawing.Rectangle

The bounding Rectangle to paint into.

source
Type: System.Windows.Forms.CurrencyManager

The CurrencyManager of the DataGrid the that contains the column.

rowNum
Type: System.Int32

The number of the row in the underlying data table.

backBrush
Type: System.Drawing.Brush

A Brush that paints the background.

foreBrush
Type: System.Drawing.Brush

A Brush that paints the foreground color.

alignToRight
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

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft