You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从绑定DataTable的ComboBox中获取所有项的ValueMember与DisplayMember

如何从绑定DataTable的ComboBox中获取ValueMember?

我想通过ComboBox生成一个DataTable,目前ComboBox是这样绑定DataTable的:

comboBox.DisplayMember = "text";
comboBox.ValueMember = "value";
comboBox.DataSource = dt;

我已经找到获取DisplayMember的方法:

string cmbText = _cmb.GetItemText(_cmb.Items[i]);

但不知道怎么从这个ComboBox里获取ValueMember,请问该怎么做?抱歉我的英语不太好。


已解决的方法:

  • 方法一:通过索引遍历,将Item转换为DataRowView后直接取字段值
for (int i = 0; i < _cmb.Items.Count; i++)
{
   var cmbDtRow = (DataRowView)_cmb.Items[i];
   string cmbText = cmbDtRow["text"].ToString();
   string cmbValue = cmbDtRow["value"].ToString();
}
  • 方法二:遍历所有DataRowView,利用ComboBox的DisplayMember和ValueMember属性动态获取值
foreach (DataRowView row in _cmb.Items)
{
   string cmbText = row[_cmb.DisplayMember].ToString();
   string cmbValue = row[_cmb.ValueMember].ToString();
}

内容的提问来源于stack exchange,提问作者insung Jang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 20:12:09