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 = "商品品牌";
}
}
大虾们帮我看看
{
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 = "商品品牌";
}
}
大虾们帮我看看
解决方案 »
- MSchart 时间轴
- 请教,repositoryItemCheckEdit类能否访问节点
- 为什么String.Compare和sql order by 返回的结果不同?
- 怎么才能让C#程序在Vista下面出现UAC标记!
- foreach给CheckBox 默认值的问题
- 用Application Updater Block实现自动更新后,安全认证如何做(100分)?
- CollectionBase的OnRemoveComplete重载问题。在Design更改显示界面时
- 前面提了,好久没有人回答,字符问题
- 急救,怎样在WINFORM中用C#做统计图啊??
- 怎么样判断一个进程在运行并将之删除??
- [提问]请问.net里如何实现这样的异步功能
- ~求救~
这个应该写在哪里?
当我打出“combobox商品品牌.”时,提示里没有databind。
你看看DataSet里面有没有数据
还有“商品品牌”是RestGoods表中的字段么?
是的!
是的!你看看DataSet里面有没有数据
这个怎么看呢?
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,然后创建DataSet,在ComBox里DataSource里选择DataSet.表名,在DisplayMember选择列名
在Formload里写上sqlDataAdapter.Fill(DataSet)就可以了