UML Design
Preview
Introduction
The DataGrid
control is one of the worst things to work with, yet very needed. The real problem about this control is printing the grid. This class supports RightToLeft printing, colored and aligned grid printing...
Background
I had to do a project that supports RightToLeft grid printing, but all the printing classes I found just didn't do the job. So, I made a new one...
Implementation
The main class is PrinterClass
, it does the actual print. Grid
is a class holding the DataGrid
data, it's made of Cells and Headers. Cell
represents a single cell in a grid with Location, Font Alignment etc. Header
represents a single header cell (inherits Cell
).
Using the code
Grid Creation
public Grid(DataGrid TheGrid)
{
try
{
DataTable TableGrid = (DataTable)TheGrid.DataSource;
rows = TableGrid.Rows.Count;
CreateColumnStyles(TheGrid,TableGrid);
if (TheGrid.TableStyles[TableGrid.TableName].GridColumnStyles.Count>0)
columns =
TheGrid.TableStyles[TableGrid.TableName].GridColumnStyles.Count;
Headers = new Header[Columns];
SetHeaders(TheGrid,TableGrid);
Cells = new Cell[Rows,Columns];
for (int i=0;i<Rows;i++)
{
for (int j=0;j<Columns;j++)
{
Cells[i,j] = new Cell(i, j, TheGrid.Font,
TheGrid.GetCellBounds(i,j), TheGrid[i,j].ToString());
}
}
SetColors(TheGrid);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
Grid Printing
private void PrintDataGrid(Graphics g)
{
StringFormat sf = new StringFormat();
if (bRightToLeft)
{
CurrentX = PrintDoc.DefaultPageSettings.PaperSize.Width -
PrintDoc.DefaultPageSettings.Margins.Right;
sf.FormatFlags = StringFormatFlags.DirectionRightToLeft;
}
else
{
CurrentX = PrintDoc.DefaultPageSettings.Margins.Left;
}
for (int i=0;i<PrintGrid.Rows;i++)
{
for (int j=0;j<PrintGrid.Columns;j++)
{
switch (PrintGrid[i,j].Alignment)
{
case HorizontalAlignment.Left:
sf.Alignment = StringAlignment.Near;
break;
case HorizontalAlignment.Center:
sf.Alignment = StringAlignment.Center;
break;
case HorizontalAlignment.Right:
sf.Alignment = StringAlignment.Far;
break;
}
if (bRightToLeft)
{
g.DrawRectangle(Pens.Black,CurrentX - PrintGrid[i,j].Width,
CurrentY,PrintGrid[i,j].Width,PrintGrid[i,j].Height);
g.DrawString(PrintGrid[i,j].Text,
PrintGrid[i,j].Font,Brushes.Black,
new RectangleF(CurrentX - PrintGrid[i,j].Width,
CurrentY, PrintGrid[i,j].Width,
PrintGrid[i,j].Height),sf);
CurrentX -=PrintGrid[i,j].Width;
}
else
{
g.DrawRectangle(Pens.Black ,CurrentX,CurrentY,
PrintGrid[i,j].Width,PrintGrid[i,j].Height);
g.DrawString(PrintGrid[i,j].Text,PrintGrid[i,j].Font,
Brushes.Black, new RectangleF(CurrentX,CurrentY,
PrintGrid[i,j].Width,PrintGrid[i,j].Height),sf);
CurrentX +=PrintGrid[i,j].Width;
}
}
if (bRightToLeft)
{
CurrentX = PrintDoc.DefaultPageSettings.PaperSize.Width -
PrintDoc.DefaultPageSettings.Margins.Right;
}
else
{
CurrentX = PrintDoc.DefaultPageSettings.Margins.Left;
}
CurrentY = CurrentY + PrintGrid[i,0].Height;
}
}
Current Todo:
- Fix - When grid is empty, header is not printed well.
- When grid is lager than the
WIDTH
of the page, continue to next.
Other DataGrid Printing Classes