0

I'm using telerik grid view and multi column combo box

i want to get the first element of combo box and insert it in the first cell of the grid view and then insert it into DB

i get a run time error when i select the combo box element

Object reference not set to an instance of an object

here is where it occurs (in the last line)

    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }

and here's my code which leads to this run time error

private void radMultiColumnComboBox3_SelectedIndexChanged(object sender, EventArgs e)
    {
        string text;
        text = radMultiColumnComboBox3.SelectedValue.ToString();
        radGridView1.Rows.Add(Text);
    }
Mostafa Bouzari
  • 9,207
  • 3
  • 16
  • 26

1 Answers1

1

Firstly, the first element of the combo box is not necessarily in the SelectedValue.

text = radMultiColumnComboBox3.SelectedValue.ToString(); //this doesn't get the first value

As the name implies, the SelectedValue is the selected value of the combo box, it is not its first element.

Secondly, it is possible for your ComboBox not to have selected value and having SelectedValue == null. Thus SelectedValue.ToString() cannot performed.

To see if a ComboBox has item, uses Items.Count

if (radMultiColumnComboBox3.MultiColumnComboBoxElement.Rows.Count > 0){
    //do something, item exists
    //check also SelectedIndex of the combo box, if it is == -1, nothing is selected
} else {
    //This combobox does not have any item
}

As to get the first element, you can do something like this,

var item = radMultiColumnComboBox3.MultiColumnComboBoxElement.Rows[0]; //item is the first element, provided the count > 0
Ian
  • 30,182
  • 19
  • 69
  • 107
  • visual studio doesn't know (item) – Mostafa Bouzari Dec 29 '15 at 15:46
  • maybe you can take as object first, then cast it to something else. I edited my answer – Ian Dec 29 '15 at 15:47
  • I did look at you're answer,What do mean by casting? – Mostafa Bouzari Dec 29 '15 at 15:51
  • Type Casting: https://msdn.microsoft.com/en-us/library/ms173105.aspx. change from one class type to another like `int b = 5; double a = (double)b;` – Ian Dec 29 '15 at 15:52
  • it still doesn't know the item – Mostafa Bouzari Dec 29 '15 at 15:54
  • Maybe you can post the image/description of the new error? – Ian Dec 29 '15 at 15:55
  • this is the error ['Telerik.WinControls.UI.RadMultiColumnComboBox' does not contain a definition for 'Items' and no extension method 'Items' accepting a first argument of type 'Telerik.WinControls.UI.RadMultiColumnComboBox' could be found (are you missing a using directive or an assembly reference?)] – Mostafa Bouzari Dec 29 '15 at 15:57
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/99224/discussion-between-ian-and-mostafa-bouzari). – Ian Dec 29 '15 at 15:57