Skip to content

Synctest-hub/how-to-change-combobox-column-dropdown-list-width-in-winforms-datagrid

Repository files navigation

How to change Combobox column dropdown list width in WinForms DataGrid (SfDataGrid)?

About the sample

This example illustrates how to change Combobox column dropdown list width in WinForms DataGrid (SfDataGrid)?

WinForms DataGrid (SfDataGrid) does not provide the direct support to change Combobox column dropdown list width. You can change ComboBox column dropdown list width by overriding OnInitializeEditElement method in GridComboBoxCellRenderer.

this.sfDataGrid1.CellRenderers.Remove("ComboBox");
this.sfDataGrid1.CellRenderers.Add("ComboBox", new GridComboBoxCellRendererExt());

public class GridComboBoxCellRendererExt : GridComboBoxCellRenderer
{
        protected override void OnInitializeEditElement(DataColumnBase column, RowColumnIndex rowColumnIndex, SfComboBox uiElement)
        {
            base.OnInitializeEditElement(column, rowColumnIndex, uiElement);
            IEnumerable comboboxSource = (IEnumerable)uiElement.DataSource;
            var comboboxlist = comboboxSource.Cast<object>().ToList();

            int maxWidth = 0;
            int temp = 0;
            foreach (var item in comboboxlist)
            {
                temp = TextRenderer.MeasureText(item.ToString(), uiElement.Font).Width;
                if (temp > maxWidth)
                {
                    maxWidth = temp;
                }
            }
            //set the combo box drop down width based on content contains in combo Box list
            uiElement.DropDownListView.ItemWidth = maxWidth;
        }
}

ComboBox dropdown width changed in SfDataGrid

Take a moment to peruse the WinForms DataGrid - Column Sizing documentation, where you can find about Column Sizing with code examples.

Requirements to run the demo

Visual Studio 2015 and above versions

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages