ComponentOne True DBGrid for .NET (2.0) Search HelpCentral 

C1TrueDBGrid.OwnerDrawCellPrint Event

Occurs before a cell is to be printed and the C1DisplayColumn.OwnerDraw is true.

[Visual Basic]

Public Event OwnerDrawCellPrint As OwnerDrawCellEventHandler

[C#]

public event OwnerDrawCellEventHandler OwnerDrawCellPrint

[Delphi]

public property OwnerDrawCellPrint: OwnerDrawCellEventHandler read remove_OwnerDrawCellPrint write add_OwnerDrawCellPrint;

Remarks

This event is only raised for columns in which the C1DisplayColumn.OwnerDraw property is set to True.

Example

The following code creates owner-drawn cells when previewing and printing:

·      Visual Basic

       Private Sub C1TrueDBGrid1_OwnerDrawCellPrint(ByVal sender As Object, ByVal e As C1.Win.C1TrueDBGrid.OwnerDrawCellEventArgs) Handles C1TrueDBGrid1.OwnerDrawCellPrint

           If e.Col = 0 Then

               ' Create a gradient brush, blue to red.

               Dim pt1, pt2 As Point

               pt1 = New Point(e.CellRect.X, e.CellRect.Y)

               pt2 = New Point(e.CellRect.Right, e.CellRect.Y)

               Dim linGrBrush As System.Drawing.Drawing2D.LinearGradientBrush

               linGrBrush = New System.Drawing.Drawing2D.LinearGradientBrush(pt1, pt2, Color.Blue, Color.Red)

               Dim rt As RectangleF

               rt = New RectangleF(e.CellRect.X, e.CellRect.Y, e.CellRect.Width, e.CellRect.Height)

               ' Fill the cell rectangle with the gradient.

               e.Graphics.FillRectangle(linGrBrush, e.CellRect)

               Dim whiteBR As Brush

               whiteBR = New SolidBrush(Color.White)

               Dim dispCol As C1.Win.C1TrueDBGrid.C1DisplayColumn

               dispCol = Me.C1TrueDBGrid1.Splits(0).DisplayColumns(e.Col)

               ' Center the text horizontally.

               Dim sfmt As New StringFormat()

               sfmt.Alignment = StringAlignment.Center

               ' Draw the text.

               e.Graphics.DrawString(dispCol.DataColumn.CellText(e.Row), dispCol.Style.Font, whiteBR, rt, sfmt)

               whiteBR.Dispose()

               ' Let the grid know the event was handled.

               e.Handled = True

           End If

       End Sub

·      C#

       private void c1TrueDBGrid1_OwnerDrawCellPrint(object sender, C1.Win.C1TrueDBGrid.OwnerDrawCellEventArgs e)

       {

           if ( e.Col = 0 )

           {

               // Create a gradient brush, blue to red.

               Point pt1, pt2;

               pt1 = new Point[e.CellRect.X, e.CellRect.Y];

               pt2 = new Point[e.CellRect.Right, e.CellRect.Y];

               System.Drawing.Drawing2D.LinearGradientBrush linGrBrush;

               linGrBrush = new System.Drawing.Drawing2D.LinearGradientBrush(pt1, pt2, Color.Blue, Color.Red);

               RectangleF rt;

               rt = new RectangleF(e.CellRect.X, e.CellRect.Y, e.CellRect.Width, e.CellRect.Height);

               // Fill the cell rectangle with the gradient.

               e.Graphics.FillRectangle(linGrBrush, e.CellRect);

               Brush whiteBR;

               whiteBR = new SolidBrush(Color.White);

               C1.Win.C1TrueDBGrid.C1DisplayColumn dispCol;

               dispCol = this.C1TrueDBGrid1.Splits[0].DisplayColumns[e.Col];

               // Center the text horizontally.

               StringFormat  sfmt = new StringFormat();

               sfmt.Alignment = StringAlignment.Center;

               // Draw the text.

               e.Graphics.DrawString(dispCol.DataColumn.CellText[e.Row], dispCol.Style.Font, whiteBR, rt, sfmt);

               whiteBR.Dispose();

               // Let the grid know the event was handled.

               e.Handled = true;

           }

       }

·      Delphi

       procedure TWinForm.C1TrueDBGrid1_OwnerDrawCellPrint(sender: System.Object; e: C1.Win.C1TrueDBGrid.OwnerDrawCellEventArgs);

       var

         pt1, pt2: Point;

         linGrBrush: System.Drawing.Drawing2D.LinearGradientBrush;

         rt: RectangleF;

         whiteBR: Brush;

         dispCol: C1.Win.C1TrueDBGrid.C1DisplayColumn;

         sfmt: StringFormat;

       begin

         if e.Col = 0 then

         begin

           // Create a gradient brush, blue to red.

           pt1 := Point.Create(e.CellRect.X, e.CellRect.Y);

           pt2 := Point.Create(e.CellRect.Copy, e.CellRect.Y);

           linGrBrush := System.Drawing.Drawing2D.LinearGradientBrush.Create(pt1, pt2, Color.Blue, Color.Red);

           rt := RectangleF.Create(e.CellRect.X, e.CellRect.Y, e.CellRect.Width, e.CellRect.Height);

           // Fill the cell rectangle with the gradient.

           e.Graphics.FillRectangle(linGrBrush, e.CellRect);

           whiteBR := SolidBrush.Create(Color.White);

           dispCol := Self.C1TrueDBGrid1.Splits[0].DisplayColumns[e.Col];

           // Center the text horizontally.

           sfmt := StringFormat.Create;

           sfmt.Alignment := StringAlignment.Center;

           // Draw the text.

           e.Graphics.DrawString(dispCol.DataColumn.CellText(e.Row), dispCol.Style.Font, whiteBR, rt, sfmt);

           whiteBR.Dispose;

           // Let the grid know the event was handled.

           e.Handled := True;

         end;

       end;

For a complete example, see Owner-Drawn Cells.

See Also

C1TrueDBGrid Class | C1TrueDBGrid Members | C1.Win.C1TrueDBGrid Namespace


Send comments about this topic to ComponentOne.
Copyright © ComponentOne LLC. All rights reserved.