I have a problem about printing datagridview in C#. Is there any example about printing with vertical columns. Lots of example contains horizontal columns.


Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Datta KharadPosted Nov 2, 2011, 9:46 AM
This code will help you to display Vertically Column as like above diagram.
Please try below code....
private void frmDataGridTesting_Load(object sender, EventArgs e)
{
dtSam = new DataTable();
string sSQLTemp = "Your Select Query";
dtSam = oDBTools.SetSelectQuery(sSQLTemp);
ArrayList columnHeading=new ArrayList () ;
foreach (DataColumn dc in dtSam.Columns)
{
columnHeading.Add(dc.ColumnName);
}
if (columnHeading.Count > 0)
{
dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
dataGridView1.ColumnHeadersHeight = 50;
//dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader;
}
dataGridView1.DataSource = dtSam;
}
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex == -1 && e.ColumnIndex >= 0)
{
e.PaintBackground(e.ClipBounds, true);
Rectangle rect = this.dataGridView1.GetColumnDisplayRectangle(e.ColumnIndex, true);
Size titleSize = TextRenderer.MeasureText(e.Value.ToString(), e.CellStyle.Font);
if (this.dataGridView1.ColumnHeadersHeight < titleSize.Width)
{
this.dataGridView1.ColumnHeadersHeight = titleSize.Width;
}
e.Graphics.TranslateTransform(0, titleSize.Width);
e.Graphics.RotateTransform(-90.0F);
e.Graphics.DrawString(e.Value.ToString(), this.Font, Brushes.Black, new PointF(rect.Y - (dataGridView1.ColumnHeadersHeight - titleSize.Width), rect.X));
e.Graphics.RotateTransform(90.0F);
e.Graphics.TranslateTransform(0, -titleSize.Width);
e.Handled = true;
}
}
If you got correct solution then mark as Accepted Answer.
alan kumarPosted Mar 18, 2014, 7:43 AM
the question is how to print it in c# windows forms?
kaan yigidoPosted Feb 5, 2012, 5:12 PM
Datta KharadPosted Dec 16, 2011, 9:34 AM
Did u get correct answer or not?