Resize popup for ComboBoxColumn to fit items

2 posts, 1 answers
  1. Justin
    Justin avatar
    8 posts
    Member since:
    Aug 2011

    Posted 04 Apr 2012 Link to this post

    Hi,

    Can I resize the popup in a ComboBoxColumn to fit the items in the list? I know I can use the:

    Popup.MinimumSize
    Popup.Size

     But is there a way to autosize to bestfit the contents of the list?

    Kind Regards
    Justin


  2. Answer
    Ivan Petrov
    Admin
    Ivan Petrov avatar
    705 posts

    Posted 09 Apr 2012 Link to this post

    Hi Justin,

    Thank you for writing.

    You can achieve this by setting the DropDownWidth of the editor's element. You have to first measure the width you will need and then assign it to the property. Here is how to do this inside the CellEditorInitialized event:
    private void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
    {
      RadDropDownListEditor editor = e.ActiveEditor as RadDropDownListEditor;
     
      if (editor == null)
      {
        return;
      }
     
      RadDropDownListElement element = editor.EditorElement as RadDropDownListEditorElement;
     
      int scrolBarWidth = 0;
     
      if (element.DefaultItemsCountInDropDown < element.Items.Count)
      {
        scrolBarWidth = 35;
      }
     
      foreach (RadListDataItem item in element.Items)
      {
        string text = item.Text;
        Size size = TextRenderer.MeasureText(text, element.Font);
     
        if (element.DropDownWidth < size.Width)
        {
          element.DropDownWidth = size.Width + scrolBarWidth;
        }
      }
    }

    I hope this will be useful for you. Should you have further questions, I would be glad to help.

    Kind regards,
    Ivan Petrov
    the Telerik team
    RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
Back to Top