Setting DropDown list width of DataGridView ComboBoxColumn - WinForms
Asked Answered
T

4

5

I'm having a datagridview with combobox column in it. This column is databound. I want to set the width of the dropdown list as per the largest item width in the list. For a normal combobox to achieve the same I've used a extension method which will set the width of the combo box by finding the largest width item in the list. This is done in the DropDown event of the combobox.

Now in DataGridView combobox column I want to achieve the same. How can I get the DropDown event in this case? Please let me if there is any other way to achieve the same?

Trueblue answered 11/10, 2010 at 11:18 Comment(1)
@Vyas: Can you please tell me what code you want here? I hope I'm clear with my question that I want to set the width of a dropdown list for a combobox column in datagridview. Do you want me to put up the code for creating datagridview and combobox column in it? Or code for setting auto size of drop down list in normal combo box?Trueblue
T
7

After little bit of investigation, I've found the answer for this.

I'm setting the datasource to the combobox column of the datagridview. So, after setting the datasource I'm finding the width of the largest item in the datatable for the value which is set as DisplayMember of the column. I'm using the same logic mentioned in the link given above in my question, instead of doing at DropDown event, I'm doing it while setting the Datasource, which is onetime. In the link given above in my question was setting the width of the drop down list every time drop down list is shown. So, in a way my approach looks good.

Here, how I done this:

// This line is picked up from designer file for reference
  DataGridViewComboBoxColumn CustomerColumn; 

  DataTable _customersDataTable = GetCustomers();

  CustomerColumn.DataSource = _customersDataTable;
  CustomerColumn.DisplayMember = Customer_Name;
  CustomerColumn.ValueMember = ID;

  var graphics = CreateGraphics();

  // Set width of the drop down list based on the largest item in the list
  CustomerColumn.DropDownWidth = (from width in
                         (from DataRow item in _customersDataTable.Rows
                          select Convert.ToInt32(graphics.MeasureString(item[Customer_Name].ToString(), Font).Width))
                       select width).Max();
Trueblue answered 11/10, 2010 at 13:30 Comment(1)
Thanks for this. I had exactly the same problem, albeit with a VB.NET application. +1Wing
P
1

You can try setting the AutoSizeMode of the column to AllCellsExceptHeader, or AllCells. You can also set the MinimumWidth of the column if auto-sizing it causes it to become too narrow.

Pewee answered 11/10, 2010 at 13:10 Comment(1)
I've fixed column width. I want to set the width of the drop down list only. I hope your solution changes the width of the column.Trueblue
S
1

You just need to set the DropDownWith property of your DataGridView column:

this.myColumnDataGridViewComboBoxColumn.DropDownWidth = variable;

For the variable, you can use a sql query to get the larger size of the column in your table.

Sweetheart answered 26/5, 2022 at 10:35 Comment(1)
You can also set this through the designer if you only want to set a static width: DataGridView Properties > Columns > 3 points button to the right of "(Collection)" > Select the combo box column > Behavior > DropDownWidth Default is 1px, but you can set 500 for example.Guria
I
0

@JPReaddy got me most of the way there (thx @JPReddy!), but that solution only expanded the drop-down portion and not the actual column. If the goal is for the column to be the same size as the widest item in the list, you have to account for the drop-down button itself and a little bit more.

Here's what I found most visually pleasing:

// Set width of the drop down list based on the largest item in the list
int colWidth = ( int )Math.Round( ( double )( from width in
                   ( from DataRow item in _customersDataTable.Rows
                          select Convert.ToInt32( graphics.MeasureString( item[Customer_Name].ToString( ), Font ).Width ) )
                                                select width ).Max( ) + SystemInformation.VerticalScrollBarWidth + 10 );

col.DropDownWidth = col.MinimumWidth = colWidth;
Implicative answered 25/8, 2024 at 1:0 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.