Code Monkey home page Code Monkey logo

how-to-change-combobox-column-dropdown-list-width-in-winforms-datagrid's Introduction

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

how-to-change-combobox-column-dropdown-list-width-in-winforms-datagrid's People

Contributors

farjanaparveen avatar vijayarasan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.