We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on : Nov 16th 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to customize the width of the GridComboboxColumn based on its HeaderText and Items?

SfDataGrid supports ColumnSizing feature that allows you to adjust the column width based on its HeaderText or data present in the cell depending upon the type of ColumnSizer that you have defined either at the SfDataGrid or column level. You cannot adjust the width of the GridComboBoxColumn based on the Items loaded from an ItemsSource of that column by using the ColumnSizer directly.

The following screenshot displays the behavior of the Auto ColumnSizer, as width of the column is calculated based on the HeaderText and cell content of the GridComboBoxColumn.

Figure 1: Column width set based on Auto ColumnSizer

In the SfDataGrid, you can adjust the width of GridComboBoxColumn based on the items loaded from an ItemsSource of that column. To achieve this, customize the column width based on the loaded items in GridComboBoxColumn by deriving a new class from the GridColumnSizer class and overriding its CalculateCellWidth () virtual method. The following code example illustrates how to derive a new class from GridColumnSizer and override its CalculateCellWidth () virtual method.

C#

The above CalculateCellWidth () method is invoked to calculate the width of each column in the SfDataGrid. You can customize the column width based on the items in GridComboBoxColumn as follows.

C#

You can get each item of GridComboBoxColumn by accessing an ItemsSource of the corresponding column and comparing the length of each item to the others in the collection and storing the item having the maximum length. By using the MeasureText () method, you can get the corresponding Height and Width of the maximum combo item text.

Assign the instance of the above CustomColumnSizer class to SfDataGrid’s GridColumnSizer property as illustrated in the following code example.

C#

The above customized width is applied to GridComboBoxColumn as displayed in the following screenshot.

Figure 2: Customized column width applied to Country column based on its loaded items

Sample Links

WPF

WinRT

WP-WinRT

UWP

Article ID: Published Date: Last Revised Date: Platform: Control: .NET Framework:
5078 05/27/2015 01/12/2016 Windows Phone SfDataGrid 4.5
Did you find this information helpful?
Add Comment
You must log in to leave a comment

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon