Mostafa Bouzari
Mostafa Bouzari

Reputation: 10240

How to get the first element of multicolumn combo box into grid view?

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);
    }

Upvotes: 0

Views: 589

Answers (1)

Ian
Ian

Reputation: 30813

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

Upvotes: 1

Related Questions