Wrap text in a column while printing - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Wrap text in a column while printing

arrowanaarrowana Posts: 2Member
Hi,

I found a very good piece of code for printing the data gridview. But i am unable to wrap the data in a column if it is too long. Can any one please help me in letting me know what I am missing or how to wrap the data in a column or hide a column

Font fnt = dataGridView.Font;
Pen pen = new Pen(Color.FromArgb(0, 0, 0));
Graphics gfx = e.Graphics;
gfx.PageUnit = System.Drawing.GraphicsUnit.Display;

SolidBrush foreBrush = new SolidBrush(this.dataGridView.DefaultCellStyle.ForeColor);
SolidBrush selectedRowBrush = new SolidBrush(Color.White);

int yMargin = 0;
int xMargin = 0;
int intRowHeight = this.dataGridView.Columns[0].HeaderCell.Size.Height;
int xPoint = xMargin;
int yPoint = yMargin;

int[] colPositions = new int[dataGridView.Columns.Count + 1];
int intCount = 0;
SizeF CaptionSize = gfx.MeasureString(dataGridView.Columns[0].HeaderText, fnt);
Size s = new Size((Convert.ToInt32(CaptionSize.Width) + 1), Convert.ToInt32(CaptionSize.Height));
Rectangle rect = new Rectangle(new Point(xMargin, yMargin), s);
yPoint += s.Height;
yMargin += s.Height;

Font headerFont = new Font(fnt, FontStyle.Bold);
SolidBrush selectedHeaderBrush = new SolidBrush(Color.LightGray);

//Header loop
foreach (DataGridViewColumn dgvc in dataGridView.Columns)
{
if (xPoint < dataGridView.Bounds.Width && xPoint < e.MarginBounds.Right - 100)
{
colPositions[intCount] = xPoint;
Rectangle rectColumn = new Rectangle(xPoint, yMargin, dgvc.Width, fnt.Height);
if (dgvc.Width > 0 && dgvc.HeaderText != string.Empty)
{
gfx.FillRectangle(selectedHeaderBrush, new Rectangle(xPoint, yMargin, dgvc.Width, dgvc.HeaderCell.Size.Height));
xPoint += dgvc.Width;
//Draw Vertical Line of column
gfx.DrawLine(pen, colPositions[intCount], yPoint, colPositions[intCount], yPoint + intRowHeight);
rectColumn = new Rectangle(colPositions[intCount], yPoint + 2, dgvc.Width + 2, fnt.Height + 1);

}

//Prints the column header text.
gfx.DrawString(dgvc.HeaderText, headerFont, Brushes.Black, rectColumn, StringFormat.GenericDefault);
intCount++;
}
else
break;
}
colPositions[intCount] = xPoint;

//end vertical line
gfx.DrawLine(pen, colPositions[intCount], yPoint, colPositions[intCount], yPoint + intRowHeight);

//Draw the first horizontal line.
gfx.DrawLine(pen, xMargin, yPoint, xPoint, yPoint);
yPoint += this.dataGridView.Columns[0].HeaderCell.Size.Height;

//Draw the second horizontal line. After this line data gets printed
gfx.DrawLine(pen, xMargin, yPoint - 1, xPoint, yPoint - 1);
intRowHeight = this.dataGridView.Rows[0].Height;

//Data loop
for (int row = 0; m_gridRowsCount < dataGridView.Rows.Count; row++, m_gridRowsCount++)
{
if (row >= MAXROWSTOPRINT)
{
break;
}
intCount = 0;
foreach (DataGridViewCell dgvc in dataGridView.Rows[m_gridRowsCount].Cells)
{
if (colPositions[intCount] < dataGridView.Bounds.Width && colPositions[intCount] < e.MarginBounds.Right - 100)
{
Rectangle rectColumn = new Rectangle(colPositions[intCount], yPoint, dgvc.Size.Width, fnt.Height);
if (dgvc.Size.Width > 0)
{
gfx.FillRectangle(selectedRowBrush, new Rectangle(colPositions[intCount], yPoint, dgvc.Size.Width, fnt.Height + 4));

//Draw Vertical Line of column
gfx.DrawLine(pen, colPositions[intCount], yPoint, colPositions[intCount], yPoint + intRowHeight);
rectColumn = new Rectangle(colPositions[intCount], yPoint + 2, dgvc.Size.Width + 2, fnt.Height + 1);
}
if (dgvc.ValueType.Equals(typeof(DateTime)) && dgvc.Value != DBNull.Value)
gfx.DrawString(Convert.ToDateTime(dgvc.Value).ToString("dd/MM/yyyy"), fnt, Brushes.Black, rectColumn, StringFormat.GenericDefault);
else
//Prints the column header text.
gfx.DrawString(Convert.ToString(dgvc.Value), fnt, Brushes.Black, rectColumn, StringFormat.GenericDefault);
intCount++;
}
else
break;
}
//Draw the last vertical line.
gfx.DrawLine(pen, colPositions[intCount], yPoint, colPositions[intCount], yPoint + intRowHeight);

yPoint += dataGridView.Rows[m_gridRowsCount].Height;

//if (intCount < colPositions.Length)
//Draw the first horizontal line.
gfx.DrawLine(pen, xMargin, yPoint - 1, colPositions[intCount], yPoint - 1);
}

gfx.Dispose();
e.Graphics.Dispose();
m_noOfPagesToPrint--;
if (m_noOfPagesToPrint <= 0)
{
m_gridRowsCount = m_noOfPagesToPrint = m_printedRows = 0;
m_noOfPagesToPrint = dataGridView.Rows.Count / MAXROWSTOPRINT;
if (dataGridView.Rows.Count % MAXROWSTOPRINT > 0)
m_noOfPagesToPrint++;

e.HasMorePages = false;
}
else
e.HasMorePages = true;

Sign In or Register to comment.