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?
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
Source: Calculating ComboBox DropDownWidth in C#
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With