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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Datta KharadRank #2,135 · 668 reputationPosted Nov 2, 2011, 5:46 AM EDT
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.
Datta KharadRank #2,135 · 668 reputationPosted Dec 16, 2011, 4:34 AM EST
Did u get correct answer or not?
kaan yigido3 reputationPosted Feb 5, 2012, 12:12 PM EST
alan kumar31 reputationPosted Mar 18, 2014, 3:43 AM EDT
the question is how to print it in c# windows forms?