如何从绑定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
相关产品推荐
相关产品推荐

