在数据绑定组合框中选择项目 [英] Selecting item in databound combobox

查看:181
本文介绍了在数据绑定组合框中选择项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



简介:


  1. 我有一个继承自ComboBox类的构造函数中的一些数据绑定:



 
var mdl = new Model();

ValueMember =id;
DisplayMember =unit;
DataSource = mdl.getUnits();


  1. 然后我有另一个带有editIngridient函数的表单。函式如下:



 
public bool editIngridient(int id)
{
currentId = id;

var row = mdl.getIngridient(id);

txtIngridient.Text =(string)row [ingridient];
cmbUnit.ID =(int)row [unitId];
numNotifyQty.Value =(int)row [notifyQty];

ShowDialog();

return true;
}




  1. 现在,当弹出窗体,文本框和数字框填充


  2. 如果我将运行combobox数据绑定函数作为editIngridient函数中的第一行 - 一切都很好。


请指向我的愚蠢。



很多!

解决方案

YOu没有说你的dataSource是什么,但我假设DataTable,所以你可以做: p>

  DataRowView rowData = comboBox1.SelectedItem as DataRowView; 
int id = Convert.ToInt32(rowData [id]);
string name = rowData [unit]。ToString();


I believe this question is kinda new-bie, but I can't solve it in correct way.

Brief description:

  1. I have an inherited from ComboBox class that does some data bindings in constructor:

    var mdl = new Model();

    ValueMember = "id";
    DisplayMember = "unit";
    DataSource = mdl.getUnits();

All good here. The combobox is filled by required data.

  1. Then I have another form with a function editIngridient. The function is following;

    public bool editIngridient(int id)
    {
    currentId = id;

    var row = mdl.getIngridient(id);

    txtIngridient.Text = (string)row["ingridient"];
    cmbUnit.ID = (int)row["unitId"];
    numNotifyQty.Value = (int) row["notifyQty"];

    ShowDialog();

    return true;
    }

  1. Now, when the form popups, textbox and number box filled by needed values, while combobox is filled by first value.

  2. If I will run the combobox data bind function as the first line inside editIngridient function - all works good.

Please point me to my stupidity.

Thanks a lot!

解决方案

YOu didnt say what is your dataSource, but I assume thats DataTable, so you can do it:

DataRowView rowData = comboBox1.SelectedItem as DataRowView;
int id = Convert.ToInt32(rowData["id"]);
string name = rowData["unit"].ToString();

这篇关于在数据绑定组合框中选择项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆