Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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.

C#
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:-

C#
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);</pre>

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 :-

C#
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++;
}


private void printDocument1_BeginPrint(object sender, PrintEventArgs e)

{

try
{
iRow = 0;
strFormat1 = new StringFormat();
strFormat1.Alignment = StringAlignment.Near;
strFormat1.LineAlignment = StringAlignment.Center;
Posted
Updated 26-Mar-14 2:03am
v2
Comments
Richard MacCutchan 26-Mar-14 7:55am    
You need to give some proper details about your problem. We cannot work out from that repeated statement what your problem is.
alanmaster 26-Mar-14 7:58am    
Additional information moved to question.
lukeer 26-Mar-14 8:22am    
Please, don't repost[^]. Improve your question[^] instead. (This one would be preferrable to the original since it already has answers.)
alanmaster 26-Mar-14 8:34am    
it don't have answer. How to print in c# from right to left top to bottom side.
lukeer 26-Mar-14 8:55am    
Me neither. Just trying to help you getting used to the local habits (which, I hope, will increase your chance of getting a usable answer from someone).

For your question: Have you tried drawing everything just as you would on screen, and letting the printer driver do the rotation?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900