0
Reply

How to print vertcally the gridview headings.

alan kumar

alan kumar

Mar 26 2014 8:57 AM
932
category item bedsheet pillowcover curtain
     
     
     
     
I was able to show my 43 dynamic columns in datagridview in c# vertically left to right bottom-up,but during printing I was not able to print it has I have shown it in datagridview.

strFormat1.FormatFlags = StringFormatFlags.DirectionVertical;

By using the above line, it prints right to left and top to bottom, which is difficult to read.
My question is how to print from left to right vertical headings.
To show in gridview I have used the below code in form load method :-
 
DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing;             DataGridView1.ColumnHeadersHeight = 50;             DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader;               // Here we attach an event handler to the cell painting event             DataGridView1.CellPainting += new DataGridViewCellPaintingEventHandler(DataGridView1_CellPainting);
 private void DataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{

// check that we are in a header cell!
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);

// This is the key line for bottom alignment - we adjust the PointF based on the
// ColumnHeadersHeight minus the current text width. ColumnHeadersHeight is the
// maximum of all the columns since we paint cells twice - though this fact
// may not be true in all usages!
e.Graphics.DrawString(e.Value.ToString(), this.Font, Brushes.Black, new PointF(rect.Y - (sbsDataGridView1.ColumnHeadersHeight - titleSize.Width), rect.X));

// The old line for comparison
//e.Graphics.DrawString(e.Value.ToString(), this.Font, Brushes.Black, new PointF(rect.Y, rect.X));


e.Graphics.RotateTransform(90.0F);
e.Graphics.TranslateTransform(0, -titleSize.Width);

e.Handled = true;
}

}
 
For printing :-
foreach (DataGridViewColumn GridCol in DataGridView1.Columns)                         {                                   e.Graphics.FillRectangle(new SolidBrush(Color.LightGray),                                 new Rectangle((int)arrColumnLefts[iCount], iTopMargin,                                 (int)arrColumnWidths[iCount], iHeaderHeight));                               e.Graphics.DrawRectangle(Pens.Black,                                 new Rectangle((int)arrColumnLefts[iCount], iTopMargin,                                 (int)arrColumnWidths[iCount], iHeaderHeight));                                  e.Graphics.DrawString(GridCol.HeaderText, GridCol.InheritedStyle.Font,                                 new SolidBrush(GridCol.InheritedStyle.ForeColor),                                 new RectangleF((int)arrColumnLefts[iCount], iTopMargin,                                 (int)arrColumnWidths[iCount], iHeaderHeight), strFormat1);                                iCount++;                         }

Collapse | Copy Code
private void printDocument1_BeginPrint(object sender, PrintEventArgs e)           {               try             {                 iRow = 0;                 strFormat1 = new StringFormat();                 strFormat1.Alignment = StringAlignment.Near;                 strFormat1.LineAlignment = StringAlignment.Center;                 strFormat1.Trimming = StringTrimming.EllipsisCharacter;                 strFormat1.FormatFlags = StringFormatFlags.DirectionVertical;                    arrColumnLefts.Clear();                 arrColumnWidths.Clear();                 iCellHeight = 0;                  //iCount = 0;                 bFirstPage = true;                 bNewPage = true;                    //Calculating Total Widths                 iTotalWidth = 0;                 foreach (DataGridViewColumn dgvGridCol in DataGridView1.Columns)                 {                     iTotalWidth += dgvGridCol.Width;                 }             }             catch (Exception ex)             {                 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);             }         }

Somewhere, I have read that after using this strFormat1.FormatFlags = StringFormatFlags.DirectionVertical, I have to again rotate it but I don't know how to do it.

My question is long because of the code that I have posted but it is necessary to elaborate my problem.
Pls reply as soon as possible.