I've got a DataGridView
with a ComboBox
in it that might contain some pretty large strings. Is there a way to have the drop down list expand itself or at least wordwrap the strings so the user can see the whole string without me having to resize the ComboBox
column width?
Asked
Active
Viewed 1.5k times
12

Breeze
- 2,010
- 2
- 32
- 43

Isaac Bolinger
- 7,328
- 11
- 52
- 90
3 Answers
12
This is very elegant solution:
private void AdjustWidthComboBox_DropDown(object sender, System.EventArgs e)
{
ComboBox senderComboBox = (ComboBox)sender;
int width = senderComboBox.DropDownWidth;
Graphics g = senderComboBox.CreateGraphics();
Font font = senderComboBox.Font;
int vertScrollBarWidth =
(senderComboBox.Items.Count>senderComboBox.MaxDropDownItems)
?SystemInformation.VerticalScrollBarWidth:0;
int newWidth;
foreach (string s in senderComboBox.Items)
{
newWidth = (int) g.MeasureString(s, font).Width
+ vertScrollBarWidth;
if (width < newWidth )
{
width = newWidth;
}
}
senderComboBox.DropDownWidth = width;
}
Adjust combo box drop down list width to longest string width http://www.codeproject.com/KB/combobox/ComboBoxAutoWidth.aspx

djv
- 15,168
- 7
- 48
- 72

Nemanja Vujacic
- 925
- 5
- 14
- 25
8
Here's what I did to solve this, works great...
public class ImprovedComboBox : ComboBox
{
public ImprovedComboBox()
{
}
public object DataSource
{
get { return base.DataSource; }
set { base.DataSource = value; DetermineDropDownWidth(); }
}
public string DisplayMember
{
get { return base.DisplayMember; }
set { base.DisplayMember = value; DetermineDropDownWidth(); }
}
public string ValueMember
{
get { return base.ValueMember; }
set { base.ValueMember = value; DetermineDropDownWidth(); }
}
private void DetermineDropDownWidth()
{
int widestStringInPixels = 0;
foreach (Object o in Items)
{
string toCheck;
PropertyInfo pinfo;
Type objectType = o.GetType();
if (this.DisplayMember.CompareTo("") == 0)
{
toCheck = o.ToString();
}
else
{
pinfo = objectType.GetProperty(this.DisplayMember);
toCheck = pinfo.GetValue(o, null).ToString();
}
if (TextRenderer.MeasureText(toCheck, this.Font).Width > widestStringInPixels)
widestStringInPixels = TextRenderer.MeasureText(toCheck, this.Font).Width;
}
this.DropDownWidth = widestStringInPixels + 15;
}
}

James McDonnell
- 3,600
- 1
- 20
- 26

Isaac Bolinger
- 7,328
- 11
- 52
- 90
-
Thank you. I chose to modify DropDownWidth property in the DropDown event. – Thomas Eyde Aug 10 '11 at 14:45
-
It might be a better idea to subscribe for `DataSourceChanged`, `ValueMemberChanged` and `DisplayMemberChanged` events instead of property hiding. – Danylo Yelizarov Nov 29 '16 at 01:29
-
Since it's a class override, the cleanest way is to just add an override for `OnDropDown` to add the call there before executing `base.OnDropDown(e)`. And btw, for that final `if >` check, just store that width in a variable instead of calling that calculation twice. – Nyerguds Dec 10 '16 at 10:38
-
1By the way, the "+15" thing to allow for the scrollbar can be enhanced by actually checking if it's needed, and by fetching the actual real scrollbar width: `if (this.Items.Count * this.ItemHeight > this.DropDownHeight) widestStringInPixels += SystemInformation.VerticalScrollBarWidth;` – Nyerguds Dec 10 '16 at 10:58
-3
Not that I know of, although some browsers are smart enough to expand the width of the drop down menu beyond that of the box if it is needed. I know Firefox and Chrome can do it if you're able to control your user base a little.
If you really are desperate how about a flash based combo box posting data back to html?

Hawxby
- 2,746
- 21
- 29
-
Was looking for a winforms answer. I should add winforms to the question. Sorry for the confusion. – Isaac Bolinger Jan 14 '11 at 04:45