ListBox没有更新组合框索引更改

时间:2016-07-09 15:10:50

标签: c# winforms combobox listbox

我的组合框确定列表框中显示的项目。但是,当索引更改时,列表框不会更新。

private void cmbProdType_SelectedIndexChanged(object sender, EventArgs e)
{            
    String query = "Select * From Product Where ProductType = '"+cmbProdType.SelectedValue+"'";    
    Prods = db.GetDataTable(query);
    dataGridView1.DataSource = Prods;
    foreach (DataRow dr in Prods.Rows)
    {
        ProdName.Add(dr[1].ToString());
        PrevCount.Add(int.Parse(dr[2].ToString()));                
    }
    listBox1.DataSource = ProdName;
    listBox2.DataSource = PrevCount;       
}    

datagridview会更新,因此它会排除查询错误。

1 个答案:

答案 0 :(得分:1)

您还需要设置DataTextFieldDataValueField

listBox1.DataSource = ProdName;
listBox1.DataTextField = "Value";
listBox1.DataValueField = "Key";

如果是ASP.NET,那么您需要拨打DataBind()

listBox1.DataBind();