如何从SQL数据库中获取数据以存储到组合框中 - C#

时间:2016-01-29 16:35:20

标签: c# .net

如何在comboBox上获取插座ID的值? 这是我从数据库中获取值并将其存储在组合框中的代码。

public partial class Addstock : Form
{
    String connectionString = ConfigurationManager.ConnectionStrings["TCConnectionString"].ConnectionString;
    List<BOStockTransfer> StockList = new List<BOStockTransfer>();
    int updateIndex = -1;

    public Addstock()
    {
        InitializeComponent();
    }

    private void Addstock_Load(object sender, EventArgs e)
    {
        loadstock();
        GetOutlets();
        Getproduct();
        GetGetWH();
        cmdoutletID.Visible = false;
        lbloutid.Visible = false;
        cmdwh.Visible = false;
        lblwh.Visible = false;
    }

    private void GetOutlets()
    {


        BOStockTransfer obj_StockTransfer = new BOStockTransfer();
        DataSet ds_OutletList = obj_StockTransfer.GetOutlets(connectionString);

        if (ds_OutletList.Tables[0].Rows.Count != 0)
        {
            cmdoutletID.DataSource = ds_OutletList.Tables[0];
            cmdoutletID.DisplayMember = "outletId";
        }

    }

My Database

感谢您的帮助!

3 个答案:

答案 0 :(得分:0)

你正在设置:

cmdoutletID.Visible = false;

是什么让组合框不可见

你必须按如下方式设置:

cmdoutletID.Visible = true;

添加图片后,列名为outletID而非outletId 所以改变你的代码如下:

private void GetOutlets()
    {
    BOStockTransfer obj_StockTransfer = new BOStockTransfer();
    DataSet ds_OutletList = obj_StockTransfer.GetOutlets(connectionString);

    if (ds_OutletList.Tables[0].Rows.Count != 0)
    {
        cmdoutletID.DataSource = ds_OutletList.Tables[0];
        cmdoutletID.DisplayMember = "outletID";
        cmdoutletID.ValueMember = "outletID";
        cmdoutletID.Enabled = true;

    }

}

答案 1 :(得分:0)

组合框有两个属性,用于确定从何处加载数据:

DisplayMember

ValueMember

“ValueMember”属性定义哪个命名字段填充ListItem的“ValueProperty”。这样,当您执行combobox.SelectedItem.Value时,您将获得存储在为“ValueProperty”指定的命名字段中的值。

假设您确定您的查询正在返回行,可能尝试“手动”添加项目,而不是依赖于自动数据绑定。

private void GetOutlets()
{
    BOStockTransfer obj_StockTransfer = new BOStockTransfer();
    DataSet ds_OutletList = obj_StockTransfer.GetOutlets(connectionString);

    cmdoutletID.DisplayMember = "outletId";
    cmdoutletID.ValueMember = "pkID";
    cmdoutletID.BeginUpdate();
    try
    {
        cmdoutletID.Items.Clear();
        foreach (var row in ds_OutletList.Tables[0].Rows)
            cmdoutletID.Items(new { outletid = row["outletid"], pkID = row["primaryKeyIDFieldName"] });

    }
    finally { cmdoutletID.EndUpdate(); }
}

答案 2 :(得分:0)

试试这个:

BOStockTransfer obj_StockTransfer = new BOStockTransfer();
        DataSet ds_OutletList = obj_StockTransfer.GetOutlets(connectionString);

        if (ds_OutletList.Tables[0].Rows.Count != 0)
        {
            cmdoutletID.DataSource = ds_OutletList.Tables[0];
            cmdoutletID.DisplayMember = "outletname";
            cmdoutletID.ValueMember = "outletId";
        }

要捕获所选值,例如点击按钮:

protected button1_Click(object o, EventArgs e)
{
  var selectedId =  cmboutletID.SelectedValue;
MessageBox.Show(selectedId);
}