GetPreferredCellSize for only ColumnHeader

Posted by: jeremie-baldy on 8 September 2017, 2:38 pm EST

  • Posted 8 September 2017, 2:38 pm EST

    Hi !

    <span id="result_box" class=""><span class="hps">My</span> <span class="hps">environment</span><span>:</span>
    <span class="hps">Windows 7 64</span> <span class="hps">bit / i5 8 Go</span>
    <span class="hps">.</span> <span class="hps">Net</span> <span class="hps">2.0</span>
    <span class="hps">Farpoint</span> <span class="hps">Spread</span> <span class="hps">WinForm</span> <span class="hps">4.0.2026</span>
    <span class="hps">VS</span> <span class="hps">2010</span></span>

     I note:

                foreach (Column c in sheet.ColumnHeader.Columns)
                    c.Width = c.GetPreferredWidth();

    Is so long.... (with 23 000 rows... ~1 minute)

     

    I need to find best size for only ColumnHeader Cells.

    Is it possible ?

    sheet.GetPreferredCellSize(0, 0); -> Takes only sheet data, not Header Cells ?

     

    Thanks

     

  • Replied 8 September 2017, 2:38 pm EST

    The ColumnHeader Class has a Columns Property that returns a collection of Column Objects upon which you can call the GetPreferredWidth Method

  • Replied 8 September 2017, 2:38 pm EST

    <BLOCKQUOTE><div><img src="/wp-content/uploads/CS_images/icon-quote.gif"> paul1960:</div><div>

    The ColumnHeader Class has a Columns Property that returns a collection of Column Objects upon which you can call the GetPreferredWidth Method

    </div></BLOCKQUOTE>

     

    Hum...

    In my first post i say:

                foreach (Column c in sheet.ColumnHeader.Columns)
                    c.Width = c.GetPreferredWidth();

    Is so long (surely it takes data sheet ~23 000 rows!)

     

    Your solution is different ?

     

     

    <span id="result_box" class="short_text"><span class="hps">Otherwise this solution </span><span class="hps"></span><span class="hps">may</span> be <span class="hps">agree</span><span class=""> ?</span></span>

                 foreach (Row r in sheet.ColumnHeader.Rows)
                {
                    foreach (Column c in sheet.ColumnHeader.Columns)
                    {
                        Cell cell = sheet.ColumnHeader.Cells[r.Index, c.Index];
                        Size s = TextRenderer.MeasureText(cell.Text, cell.Font);
                        if (sheet.Rows[r.Index].Height < s.Height)
                            sheet.Rows[r.Index].Height = s.Height;
                        if (sheet.Columns[c.Index].Width < s.Width)
                            sheet.Columns[c.Index].Width = s.Width;
                    }
                }

  • Replied 8 September 2017, 2:38 pm EST

    Your solution should work, I was thinking that there was a GetPreferredCellSize method for Cells but its actually a sheet method and won't work for a ColumnHeader Cell
Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels