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)

public protected:
virtual void Paint(
	Graphics^ g,
	Rectangle bounds,
	CurrencyManager^ source,
	int rowNum,
	Brush^ backBrush,
	Brush^ foreBrush,
	bool alignToRight
) override

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 ref class MyGridColumn: public 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 );
   }

};

public ref class Form1: public Form
{
protected:
   DataGrid^ dataGrid1;
   DataSet^ myDataSet;

private:
   void PaintCell( Object^ sender, MouseEventArgs^ e )
   {

      // Use the HitTest method to get a HitTestInfo object.
      DataGrid::HitTestInfo ^ hi;
      DataGrid^ grid = dynamic_cast<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 = dynamic_cast<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 = dynamic_cast<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 = gcnew System::Drawing::SolidBrush( Color::Blue );
         Brush^ bBrush = gcnew 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 );
      }
   }

};

.NET Framework
Available since 1.1
Return to top
Show: