Click here to Skip to main content
15,897,968 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I can print the gridview Row wise. But I want to print the gridview column wise means loop of column. Here is my code in C#
C#
strFormat = new StringFormat();
            strFormat.Alignment = StringAlignment.Center;
            strFormat.LineAlignment = StringAlignment.Center;
            strFormat.Trimming = StringTrimming.EllipsisCharacter;
            arrColumnLefts.Clear();
            arrColumnWidths.Clear();
            iCellHeight = 0;
            iRow = 0;
            bFirstPage = true;
            bNewPage = true;
            iTotalWidth = 0;
            foreach (DataGridViewColumn dgvGridCol in dataGridView1.Columns)
            {
                iTotalWidth += dgvGridCol.Width;
            }
            int iLeftMargin = e.MarginBounds.Left;
            int iTopMargin = e.MarginBounds.Top;
            bool bMorePagesToPrint = false;
            int iTmpWidth = 0;
            if (bFirstPage)
            {
                foreach (DataGridViewColumn GridCol in dataGridView1.Columns)
                {
                    iTmpWidth = (int)(Math.Floor((double)((double)GridCol.Width / (double)iTotalWidth * (double)iTotalWidth * ((double)e.MarginBounds.Width / (double)iTotalWidth))));
                    iHeaderHeight = (int)(e.Graphics.MeasureString(GridCol.HeaderText,GridCol.InheritedStyle.Font, iTmpWidth).Height) + 11;
                    arrColumnLefts.Add(iLeftMargin);
                    arrColumnWidths.Add(iTmpWidth);
                    iLeftMargin += iTmpWidth;
                }
            }
            while (iRow <= dataGridView1.Rows.Count - 1)
            {
                DataGridViewRow GridRow = dataGridView1.Rows[iRow];
                iCellHeight = GridRow.Height + 5;
                int iCount = 0;
                if (iTopMargin + iCellHeight >= e.MarginBounds.Height + e.MarginBounds.Top)
                {
                    bNewPage = true;
                    bFirstPage = false;
                    bMorePagesToPrint = true;
                    break;
                }
                else
                {
                    if (bNewPage)
                    { 
                        iTopMargin = e.MarginBounds.Top;
                        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), strFormat);
                            iCount++;
                        }
                        bNewPage = false;
                        iTopMargin += iHeaderHeight;
                    }
                    iCount = 0;               
                    foreach (DataGridViewCell Cel in GridRow.Cells)
                    {
                        if (Cel.Value != null)
                        {
                            e.Graphics.DrawString(Cel.Value.ToString(), Cel.InheritedStyle.Font,new SolidBrush(Cel.InheritedStyle.ForeColor),new RectangleF((int)arrColumnLefts[iCount], (float)iTopMargin, (int)arrColumnWidths[iCount], (float)iCellHeight), strFormat);
                        }
                        e.Graphics.DrawRectangle(Pens.Black, new Rectangle((int)arrColumnLefts[iCount],iTopMargin, (int)arrColumnWidths[iCount], iCellHeight));
                        iCount++;
                    }
                }
                iRow++;
                iTopMargin += iCellHeight;
            }
            if (bMorePagesToPrint)
                e.HasMorePages = true;
            else
                e.HasMorePages = false;
            StringFormat str = new StringFormat();
            str.Alignment = StringAlignment.Near;
            str.LineAlignment = StringAlignment.Center;
            str.Trimming = StringTrimming.EllipsisCharacter;
            Pen p = new Pen(Color.Black, 2.5f);
            string text1 = "Transection Date is";
            string text2 = "Amount is";
            string text3 = "Family Code No. is";
            System.Drawing.Font printFont = new System.Drawing.Font("Arial", 12, System.Drawing.FontStyle.Bold);
            System.Drawing.Font printFont1 = new System.Drawing.Font("Arial", 12, System.Drawing.FontStyle.Regular);
            e.Graphics.DrawString(lblShowDate.Text, printFont1, Brushes.Black, 280, 50);
            e.Graphics.DrawString(text1, printFont, Brushes.Black, 100, 50);
            e.Graphics.DrawString(lblShowAmount.Text, printFont1, Brushes.Black, 280, 70);
            e.Graphics.DrawString(text2, printFont, Brushes.Black, 100, 70);
            e.Graphics.DrawString(txtFamilyCode.Text, printFont, Brushes.Black, 580, 20);
            e.Graphics.DrawString(text3, printFont, Brushes.Black, 420, 20);
            e.Graphics.PageUnit = GraphicsUnit.Inch;



Thank You.
Posted

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