Category / Section
How to change Combobox column dropdown list width in WinForms DataGrid (SfDataGrid)?
1 min read
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; } }
Take a moment to peruse the WinForms DataGrid - Column Sizing documentation, where you can find about Column Sizing with code examples.
You can download the example from GitHub.