Brendan Gooden
Brendan Gooden

Reputation: 1551

ComboBox returning System.Data.DataRowView

My C# code is as follows

    private void offHwyManufacturer_SelectedValueChanged(object sender, EventArgs e)
    {
        var offHwyManufacturerSelected = offHwyManufacturerComboBox.SelectedValue;
        var query = "SELECT DISTINCT [EQUIPMENT TYPE] FROM ApplicationOffHwyData WHERE MANUFACTURER = '" + offHwyManufacturerSelected + "' ORDER BY [EQUIPMENT TYPE]";
        using (var adaptor = new OleDbDataAdapter(query, ConnString))
        {
            var offHwyEquipmentTypeDataTable = new DataTable();
            adaptor.Fill(offHwyEquipmentTypeDataTable);

            offHwyEquipmentTypeComboBox.DisplayMember = "[EQUIPMENT TYPE]";
            offHwyEquipmentTypeComboBox.ValueMember = "[EQUIPMENT TYPE]";
            offHwyEquipmentTypeComboBox.DataSource = offHwyEquipmentTypeDataTable;
        }
    }

offHwyManufacturerComboBox is the first combobox, and this fires the 'SelectedValueChanged' event (This displays elements correctly)

However, on the second dropdown list (Equipment Type) I'm getting System.Data.DataRowView instead of the actual value.

I'm well aware that I need to assign a DisplayMember / ValueMember, which I have done, yet it still isnt giving the right result.

If I run that SQL command manually (using the first option from 'Manufacturer' as an example) this is the result

EQUIPMENT TYPE
Compactors
Crawler Tractors
Dozers
Drilling Equipment
Excavators
Farm Equipment
Forestry Equipment
Generators, Gen Set Engines
Hydraulic Controls
Industrial Engines
Integrated Toolcarriers
Lift Trucks
Loaders
Marine Engines
Marine Gear
Material Handlers
Motor Graders
Off-Highway Trucks
Paving Equipment
Pipelayers
Scrapers, Wheel Tractors
Telehandlers
Turbine Engines
Winches
Windrow Elevators

Obviously this is only returning one row anyway, and the column name is 'EQUIPMENT TYPE'

Here is a screenshot of my WinForm app.

Am I just dense, or am I missing something completely obvious?

Upvotes: 4

Views: 686

Answers (1)

Jeremy Thompson
Jeremy Thompson

Reputation: 65544

The only things that seem odd are a) using an alphanumeric field for both DisplayMember and Value Memberand b) a space in the field name (even though you have enclosed [EQUIPMENT TYPE] in brackets).

So 1st suggestion is to fix both those potential issues like this:

var query = "SELECT DISTINCT Id, [EQUIPMENT TYPE] AS EquipmentType FROM ApplicationOffHwyData WHERE MANUFACTURER = '" + offHwyManufacturerSelected + "' ORDER BY [EQUIPMENT TYPE]";
using (var adaptor = new OleDbDataAdapter(query, ConnString))
{
    var offHwyEquipmentTypeDataTable = new DataTable();
    adaptor.Fill(offHwyEquipmentTypeDataTable);

    offHwyEquipmentTypeComboBox.DisplayMember = "EquipmentType";
    offHwyEquipmentTypeComboBox.ValueMember = "Id";
    offHwyEquipmentTypeComboBox.DataSource = offHwyEquipmentTypeDataTable;
}

The 2nd suggestion is to to use a BindingSource with its DataSource = offHwyEquipmentTypeDataTable, then set the combobox's DataSource to the BindingSource with its DisplayMember set to value and Value Member set to key. Here is rough outline of what that would look like:

BindingSource bs;
...
var offHwyEquipmentTypeDataTable = new DataTable();
adaptor.Fill(offHwyEquipmentTypeDataTable);

bs.DataSource = offHwyEquipmentTypeDataTable;
offHwyEquipmentTypeComboBox.DisplayMember = "EquipmentType";
offHwyEquipmentTypeComboBox.ValueMember = "Id";
offHwyEquipmentTypeComboBox.DataSource = bs;

Upvotes: 2

Related Questions