0

I have a datagridview combobox column. I need to dynamically adjust the width of combobox to the longest string width, so that user can read lengthy string in the combobox item. My code is

            int width = comboBox.DropDownWidth;
            Graphics g = comboBox.CreateGraphics();
            Font font = comboBox.Font;
            int vertScrollBarWidth =
                (comboBox.Items.Count > comboBox.MaxDropDownItems)
                ? SystemInformation.VerticalScrollBarWidth : 0;

            int newWidth;
            foreach (string s in ((ComboBox)sender).Items)
            {
                newWidth = (int)g.MeasureString(s, font).Width
                    + vertScrollBarWidth;
                if (width < newWidth)
                {
                    width = newWidth;
                }
            }
            comboBox.DropDownWidth = width;

But this not working. I dont think there is any problem with the code but still the width is not changed. Do I have to change any property of datagridview in order to change the width?

Nikhil Dinesh
  • 3,359
  • 2
  • 38
  • 41
Greeshma R
  • 61
  • 1
  • 5

0 Answers0