短一个。
一个名为ComDet的数据库,其中包含列cID(PK),cName,cDet,mainCate(FK),Subcat(FK)。
这假设是将数据从表ComDet获取到组合框。
DataSet ds2;
private void searchBtn_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=.\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
conn.Open();
SqlDataAdapter daSearch = new SqlDataAdapter("SELECT cName FROM ComDet", conn);
ds2 = new DataSet();
daSearch.Fill(ds2, "daSearch");
ListU.ValueMember = "cName";
ListU.DataSource = ds2.Tables["ComDet"];
ListU.DropDownStyle = ComboBoxStyle.DropDownList;
ListU.Enabled = true;
}
但是它没有用..我在哪里出错了?组合框中未显示数据库表ComDet中的Data(cName)。–
问题:你是分配数据库表名ComDet
作为DataSource
而不是DataTable
名称daSearch
来ComboBox
。
解决方案:您需要将有效DataTable
名称分配给ComboBox
as Datasource
。
替换为:
ListU.DataSource = ds2.Tables["ComDet"];
有了这个:
ListU.DataSource = ds2.Tables["daSearch"];
(要么)
ListU.DataSource = ds2.Tables[0];
完整的代码:
DataSet ds2;
private void searchBtn_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=.\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
conn.Open();
SqlDataAdapter daSearch = new SqlDataAdapter("SELECT cName FROM ComDet", conn);
ds2 = new DataSet();
daSearch.Fill(ds2, "daSearch");
ListU.ValueMember = "cName";
ListU.DataSource = ds2.Tables["daSearch"];
ListU.DropDownStyle = ComboBoxStyle.DropDownList;
ListU.Enabled = true;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句