Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
20
PDF Export, Width of columns wrong
posted

Hello,

 

if i try to Export a WebGrid to PDF, the widht of the colums is wrong. All is correct in the WebPage But the table in the PDF is to small. What can i do ?

 

 

Freddi

  • 1414
    Verified Answer
    posted

    Freddi,

    I think you might have answered your own question.  If you allow the grid columns to size themselves than the actual width of the grid column is actually zero.  Thus, when you 'export' your grid you end up with what looks like extended RowSelectors.  What that yak actually is is all the visible grid columns scrunched up as zero width columns.

    I solve this by running the grid through a method that sizes the columns 'manually'.  Here is the code all ugly and all because this editor (and the web) have a hard time with tabs:

    =============================

    public class WebGrid

    {

    const int MAX_ROWS_TO_SAMPLE = 50;public static void SetColumnWidthsByPercentage(Infragistics.WebUI.UltraWebGrid.UltraWebGrid wg)

    {

    int[ aFieldLengths = new int[wg.Columns.Count];

    foreach (Infragistics.WebUI.UltraWebGrid.UltraGridColumn col in wg.Columns)

    {

    aFieldLengths[col.Index] = 0;

    if (!col.Hidden)

    aFieldLengths[col.Index] = col.Header.Caption.Length;

    }

    foreach (Infragistics.WebUI.UltraWebGrid.UltraGridRow row in wg.Rows)

    {

    if (row != null)

    {

    foreach (Infragistics.WebUI.UltraWebGrid.UltraGridCell rowCell in row.Cells)

    {

    if (rowCell.Column.Hidden == false)

    {

    if (rowCell.Text != null)

    {

    if (rowCell.Text.Length > aFieldLengths[rowCell.Column.Index])

    aFieldLengths[rowCell.Column.Index] = rowCell.Text.Length;

    }

    }

    }

    }

    //Sample MAX_ROWS_TO_SAMPLE for header/field content size

    if (row.Index > MAX_ROWS_TO_SAMPLE) break;

    }

    int totalFieldLengths = 0;for (int i = 0; i < aFieldLengths.Length; i++)

    {

    totalFieldLengths += aFieldLengthsIdea;

    }

    foreach (Infragistics.WebUI.UltraWebGrid.UltraGridColumn col in wg.Columns)

    {

    if (col.Hidden == false)

    col.Width = Unit.Percentage((double)aFieldLengths[col.Index] / (double)totalFieldLengths); // Unit.Pixel(aFieldLengths[col.Index] * 15);

    }

    }

    }

    =============================

    I know it is a bit of a kludge, but it works till I get more time to finesse it...

    Basically, you have to set the Width property of all visible columns in the grid before exporting it.  The above code sets that width to the bigger of the column header and the largest column contents within the first  MAX_ROWS_TO_SAMPLE rows.  I use the first 50 rows in the datasource.