Search code examples
c#sqlcomboboxdatarowview

DataRowView instead of value


I get an error whenever I try to run my application because instead of getting the real value of comboBox.SelectedValue, I get a DataRowView item.

Here's the code where I'm getting the error:

private void InitDataGridView()
{
    query = "SELECT p.name, p.age FROM Person p INNER JOIN Class c ON p.idC=c.idC WHERE p.id=" 
            + comboBoxClass.SelectedValue;
    command = new SqlCommand(query, connection);
    adapter = new SqlDataAdapter(command);
    datatable = new DataTable();
    adapter.Fill(datatable);
    dataGridViewStudents.DataSource = datatable;
}

comboBoxClass.SelectedValue should return me "idC" since I set DataSource, DisplayMember and (ValueMember -> idC).

idC is the primary key (int).

The ComboBox setting:

comboBoxClass.DataSource = datatable;
comboBoxClass.DisplayMember = "className";
comboBoxClass.ValueMember = "idC";

Solution

  • Ok, I figured out what was wrong in my code. It wasn't neither the comboBox initialization nor dataGridView's but it was completely my fault on writing down a wrong query.

    I thank you all for the effort to help me.