How to set either 'Index' or 'Text' for a ComboBox

Posted by: ulrika on 8 September 2017, 12:29 pm EST

  • Posted 8 September 2017, 12:29 pm EST

    I have created a spread where some columns use a ComboBox celltype according to:


    ComboBoxCellType comboBoxCellType1 = new ComboBoxCellType();


    comboBoxCellType1.ButtonAlign = ButtonAlign.Right;


    comboBoxCellType1.EditorValue = EditorValue.ItemData;


    comboBoxCellType1.ListControl = new ListBox();


    StringCollection myList = getList();


    foreach (string strItem in myList)


    {


    comboBoxCellType1.ListControl.Items.Add(strItem);



    }


    this.fpSpread1_Sheet1.Columns.Get(1).CellType = comboBoxCellType1;


    Then, later in my code, I would like to initialize the ComboBox with an input string. If the input string is null or empty the combobox should instead display the first value in the list


    if (string.IsNullOrEmpty(myInputString))


    {


    fpSpread1_Sheet1.Cells[i, 1].Value = 0;



    }


    else


    {


    fpSpread1_Sheet1.Cells[i, 1].Text = myInputString;


    }


    This works fine when the string is empty. However, I cannot set the text to the selected input string (in my test case I check earlier to make sure that the input string is already one of the Items for this comboBox). I suspect that this is because I have set the EditorValue to Index. But I have to do this to be able to select the first item in the case where the input string is empty. How do I solve this?

  • Replied 8 September 2017, 12:29 pm EST

    Hello,


    You are correct at what the issue is. You need to convert the string to its item number equivalent. You can search the Items array in the combobox values to find the index of the string and then find the ItemData at that location to set for the Value of the cell.

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels