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 = "商品品牌";
            }
        }
大虾们帮我看看

解决方案 »

  1.   

    comboBox商品品牌.DataBind()
    这个应该写在哪里?
    当我打出“combobox商品品牌.”时,提示里没有databind。
      

  2.   

    winform的吧
    你看看DataSet里面有没有数据
    还有“商品品牌”是RestGoods表中的字段么?
      

  3.   

    winform的吧
    是的!
      

  4.   

    还有“商品品牌”是RestGoods表中的字段么?
    是的!你看看DataSet里面有没有数据
    这个怎么看呢?
      

  5.   

    设个断点到这里,或者
    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("没有数据");               }
                }
            }
      

  6.   

    一般我会怎么写.
    直接查列名效率也高.
    还有你这个功能写在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();
               }
            }
      

  7.   

    这个用控件做比较简单
    先用sqlDataAdapter创建SlecetCommand,然后创建DataSet,在ComBox里DataSource里选择DataSet.表名,在DisplayMember选择列名
    在Formload里写上sqlDataAdapter.Fill(DataSet)就可以了