简体   繁体   English

来自DataTable Winforms C#的ComboBox SelectedItem?

[英]ComboBox SelectedItem from DataTable Winforms C#?

I have a method in my DAL which populates a ComboBox from a DataTable . 我的DAL中有一个从DataTable填充ComboBox的方法。 ComboBox displays correctly and I have a 'Save' button which saves back to my DB, job's a good'un... ComboBox正确显示,并且我有一个“保存”按钮,可以将其保存回我的数据库,这是一件好事。

 public string populateLookUp(ref System.Windows.Forms.ComboBox Combo, string Id)
    {
        SqlCommand _comm = new SqlCommand();
        _comm.Parameters.AddWithValue("@id", Id);
        _comm.CommandText = "SELECT [name] FROM dbo.fnGetName(@id) ORDER BY [name]; ";   
        _comm.Connection = _conn;
        _comm.CommandTimeout = _command_timeout;

        DataTable dt = new DataTable();
        try
        {
            SqlDataReader myReader = _comm.ExecuteReader();
            dt.Load(myReader);
        }
        catch
        {
            MessageBox.Show("Unable to populate Name LookUp");
        }

        Combo.DataSource = dt;            
        Combo.DisplayMember = "name";

        foreach (DataRow dr in dt.Rows)
        {
            if (dr["company_int_name"].ToString() == Contract.Company_trans_Selling_Entity.ToString())
            {
                Combo.SelectedItem = dr["company_int_name"].ToString();
            }
        }
        return "";
    }

However obviously when I re-edit the record, this method is called again. 但是很明显,当我重新编辑记录时,再次调用此方法。 Ok, I now have written a ForEach loop which iterates over my DataTable and compares the string in the rows to the Name I am passing in. If the two match I'm setting the 好的,我现在编写了一个ForEach循环,该循环遍历DataTable并将行中的字符串与我要传递的Name进行比较。如果两者匹配,则设置

Combo.SeletedItem =  dr["company_int_name"].ToString();

However the selectedItem is not being set, presumably I'll need some sort of event to notify the property changed? 但是未设置selectedItem ,大概我需要某种事件来通知属性已更改?

Thanks 谢谢

You have to bind the Combo Box with the ValueMember function and can reflect the combo box with the matched string value as selected 您必须将组合框与ValueMember函数绑定在一起,并且可以将组合框与选定的匹配字符串值一起反映出来。

public string populateLookUp(ref System.Windows.Forms.ComboBox Combo, string Id)
{
    SqlCommand _comm = new SqlCommand();
    _comm.Parameters.AddWithValue("@id", Id);
    _comm.CommandText = "SELECT [name] FROM dbo.fnGetName(@id) ORDER BY [name]; ";   
    _comm.Connection = _conn;
    _comm.CommandTimeout = _command_timeout;

    DataTable dt = new DataTable();
    try
    {
        SqlDataReader myReader = _comm.ExecuteReader();
        dt.Load(myReader);
    }
    catch
    {
        MessageBox.Show("Unable to populate Name LookUp");
    }

    Combo.DataSource = dt;            
    Combo.DisplayMember = "name";
    Combo.ValueMember = "name";

    foreach (DataRow dr in dt.Rows)
    {
        if (dr["company_int_name"].ToString() == Contract.Company_trans_Selling_Entity.ToString())
        {
            Combo.SelectedValue = dr["company_int_name"].ToString();
        }
    }
    return "";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM