Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
4.00/5 (3 votes)
See more:
Hey guys,

I have got a code for printing content displayed in datagrid view. But as my table has 10 columns, printer is not
printing all the columns on a single page. It prints 7 columns on one page and rest on another. I tried to solve this
problem by wrapping the header text of columns so that all the columns. I changed the "columnheaderdefaultstyle" property of gridview but it didn't help. I also want to wrap the row text. Here's the code I am using :

SizeF tmpSize = new SizeF();
          Font tmpFont;
          float tmpWidth;
          TheDataGridViewWidth = 0;
          for (int i = 0; i < TheDataGridView.Columns.Count; i++)
          {
            tmpFont = TheDataGridView.ColumnHeadersDefaultCellStyle.Font;
            
            if (tmpFont == null) // If there is no special HeaderFont style, then use the default DataGridView font style
              tmpFont = TheDataGridView.DefaultCellStyle.Font;

            tmpSize = g.MeasureString(TheDataGridView.Columns[i].HeaderText, tmpFont);
            tmpWidth = tmpSize.Width;
            RowHeaderHeight = tmpSize.Height;

            for (int j = 0; j < TheDataGridView.Rows.Count; j++)
            {
              tmpFont = TheDataGridView.Rows[j].DefaultCellStyle.Font;
              if (tmpFont == null) // If the there is no special font style of the CurrentRow, then use the default one associated with the DataGridView control
                tmpFont = TheDataGridView.DefaultCellStyle.Font;

              tmpSize = g.MeasureString("Anything", tmpFont);
              RowsHeight.Add(tmpSize.Height);

              tmpSize = g.MeasureString(TheDataGridView.Rows[j].Cells[i].EditedFormattedValue.ToString(), tmpFont);
              if (tmpSize.Width > tmpWidth)
                tmpWidth = tmpSize.Width;
            }
            if (TheDataGridView.Columns[i].Visible)
              TheDataGridViewWidth += tmpWidth;
            ColumnsWidth.Add(tmpWidth);
          }



How can I solve this problem??
Please help.

Thanks
Ajinkya
Posted

1 solution

Have you tried to print it as landscape?

You will have to make some trade-offs.
From what I can see here, you want to adjust column width to maximum possible regarding the cell content, you can't get it always.
In similar case I would use landscape as default orientation and try to adjust height of row for string values, or to use smaller font for numeric values, or combination; as I said trade-offs, you must make some even with commercial reporting engines.
 
Share this answer
 
v3
Comments
ajinkya11121 14-Apr-11 5:37am    
hey, thank you very much for your reply.
I certainly understand what you mean here. To be honest, i have pretty much tried everything. But nothing seems to be helping.

Thanks again for replying.

Regards
Ajinkya

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