gpt4 book ai didi

c# - ComboBox 返回 System.Data.DataRowView

转载 作者:行者123 更新时间:2023-11-30 12:41:46 26 4
gpt4 key购买 nike

我的C#代码如下

    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 是第一个组合框,它会触发“SelectedValueChanged”事件(这会正确显示元素)

但是,在第二个下拉列表(设备类型)中,我得到的是 System.Data.DataRowView 而不是实际值。

我很清楚我需要分配一个 DisplayMember/ValueMember,我已经完成了,但它仍然没有给出正确的结果。

如果我手动运行该 SQL 命令(以“制造商”中的第一个选项为例),这就是结果

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

显然这只会返回一行,列名是'EQUIPMENT TYPE'

这是我的 WinForm 应用程序的屏幕截图。

我是不是很笨,还是漏掉了一些非常明显的东西?

最佳答案

唯一看起来奇怪的事情是 a) DisplayMemberValue Member 都使用字母数字字段和 b) 字段名称中的空格(即使您有将 [设备类型] 括在方括号中)。

所以第一个建议是像这样解决这两个潜在问题:

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

第二个建议是使用具有 DataSource = offHwyEquipmentTypeDataTable 的 BindingSource,然后将组合框的 DataSource 设置为 BindingSource,其 DisplayMember 设置为 value 和 值成员 设置为键。以下是大致的轮廓:

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

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

关于c# - ComboBox 返回 System.Data.DataRowView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36489056/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com