Column width in detailsview

4 posts, 0 answers
  1. Steve Healey
    Steve Healey avatar
    11 posts
    Member since:
    Jun 2004

    Posted 07 Nov 2012 Link to this post

    Hi Telerik,

    I have a listview with a view type of detailsview which is data bound within the code and displays 4 columns (auto generated).

    In the attached image is there a way to set columns 2,3 & 4 width to the longest field value and first column to a max of the remaining listview width thus removing the white space to the right?

    Many thanks.

    Steve.

  2. Ivan Todorov
    Admin
    Ivan Todorov avatar
    688 posts

    Posted 12 Nov 2012 Link to this post

    Hello Steve,

    Thank you for your question.

    Currently, RadListView does not provide Best Fit functionality. This has already been requested and is available in PITS as a feature request. Here is the link to the PITS item: http://www.telerik.com/support/pits.aspx#/public/winforms/12528. You can vote for it to increase its priority and subscribe to it to track its progress.

    Should you have any other questions, do not hesitate to ask.

    Greetings,
    Ivan Todorov
    the Telerik team
    Q3’12 of RadControls for WinForms is available for download (see what's new). Get it today.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Steve Healey
    Steve Healey avatar
    11 posts
    Member since:
    Jun 2004

    Posted 12 Nov 2012 Link to this post

    Hi Ivan,

    Thanks for your answer, I have added my vote.

    In the mean time is there a work around, an event that I could use to set the column width to the longest value of the data?

    Many thanks,

    Steve.
  5. Ivan Todorov
    Admin
    Ivan Todorov avatar
    688 posts

    Posted 15 Nov 2012 Link to this post

    Hi Steve,

    You can try using the TextRenderer class to measure the text of each cell and try to calculate the desired width for a column. You can add this to the MouseDoubleClick event so you can also detect if the user has double clicked near the resizing area:
    void radListView1_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        DetailListViewHeaderCellElement headerCell = this.radListView1.ElementTree.GetElementAtPoint(e.Location) as DetailListViewHeaderCellElement;
        if (headerCell != null &&
            (Math.Abs(headerCell.ControlBoundingRectangle.Right - e.Location.X) < 5 ||
            Math.Abs(headerCell.ControlBoundingRectangle.Left - e.Location.X) < 5))
         
        {
            int maxWidth = TextRenderer.MeasureText(headerCell.Data.HeaderText, headerCell.Font).Width ;
     
            foreach (ListViewDataItem item in this.radListView1.Items)
            {
                string text = String.Format("{0}", item[headerCell.Data]);
                maxWidth = Math.Max(maxWidth, TextRenderer.MeasureText(text, headerCell.Font).Width);
            }
     
            headerCell.Data.Width = maxWidth;
        }
    }

    The above approach might be a bit inaccurate but it you can tune it if needed. I hope you find it useful.

    Should you have any other questions, feel free to ask.

    Greetings,
    Ivan Todorov
    the Telerik team
    Q3’12 of RadControls for WinForms is available for download (see what's new). Get it today.
Back to Top