combobox绑定dataset,但是点击combobox下拉菜单的时候什么都没有?
- C# code
private void comboBox商品品牌_SelectedIndexChanged(object sender, EventArgs e) { string source = "server=(local)\\ SQLEXPRESS;" + "Integrated Security=True;" + "Database=Goods"; using (SqlConnection conn = new SqlConnection(source)) { conn.Open(); string select = "select * from RestGoods"; SqlDataAdapter da = new SqlDataAdapter(select, conn); //SqlCommandBuilder bu = new SqlCommandBuilder(da); DataSet ds = new DataSet(); da.Fill(ds, "RestGoods"); comboBox商品品牌.DataSource = ds.Tables["RestGoods"]; comboBox商品品牌.DisplayMember = "商品品牌"; comboBox商品品牌.ValueMember = "商品品牌"; } }
大虾们帮我看看
[解决办法]
.DataBind()
[解决办法]
comboBox商品品牌.DataBind()
[解决办法]
没绑定撒
[解决办法]
winform的吧
你看看DataSet里面有没有数据
还有“商品品牌”是RestGoods表中的字段么?
[解决办法]
设个断点到这里,或者
private void comboBox商品品牌_SelectedIndexChanged(object sender, EventArgs e)
{
string source = "server=(local)\\ SQLEXPRESS;" + "Integrated Security=True;" + "Database=Goods";
using (SqlConnection conn = new SqlConnection(source))
{
conn.Open();
string select = "select * from RestGoods";
SqlDataAdapter da = new SqlDataAdapter(select, conn);
//SqlCommandBuilder bu = new SqlCommandBuilder(da);
DataSet ds = new DataSet();
da.Fill(ds, "RestGoods");
if(ds!=null&&ds.tables.count>0&&ds.tables[0].rows.count>0)
{
messagebox.show(ds.Tables["RestGoods"].rows.count.tostring());
}
else
{
messagebox.show("没有数据");
}
}
}
[解决办法]
一般我会怎么写.
直接查列名效率也高.
还有你这个功能写在SelectedIndexChanged事件第一次肯定不妥.建议写到LOAD事件里.
private void comboBox商品品牌_SelectedIndexChanged(object sender, EventArgs e)
{
string source = "server=(local)\\ SQLEXPRESS;" + "Integrated Security=True;" + "Database=Goods";
using (SqlConnection conn = new SqlConnection(source))
{
conn.Open();
string select = "select 列名 from RestGoods"; //列名为你需要绑定到COMBOBOX的列值.
SqlDataAdapter da = new SqlDataAdapter(select, conn);
//SqlCommandBuilder bu = new SqlCommandBuilder(da);
DataSet ds = new DataSet();
da.Fill(ds, "RestGoods");
comboBox商品品牌.DataSource = ds.Tables["RestGoods"];
comboBox商品品牌.DataBind();
}
}
[解决办法]
这个用控件做比较简单
先用sqlDataAdapter创建SlecetCommand,然后创—ataSet,在ComBox里DataSource里选择DataSet.表名,在DisplayMember选择列名
在Formload里写上sqlDataAdapter.Fill(DataSet)就可以了