This is a migrated thread and some comments may be shown as answers.

Column width in detailsview

3 Answers 258 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Steve Healey
Top achievements
Rank 1
Steve Healey asked on 07 Nov 2012, 07:00 PM
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.

3 Answers, 1 is accepted

Sort by
0
Ivan Todorov
Telerik team
answered on 12 Nov 2012, 03:11 PM
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.
0
Steve Healey
Top achievements
Rank 1
answered on 12 Nov 2012, 03:40 PM
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.
0
Ivan Todorov
Telerik team
answered on 15 Nov 2012, 01:44 PM
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.
Tags
ListView
Asked by
Steve Healey
Top achievements
Rank 1
Answers by
Ivan Todorov
Telerik team
Steve Healey
Top achievements
Rank 1
Share this question
or