【发布时间】:2013-07-17 18:37:15
【问题描述】:
所以我目前有两个组合框。一个是制造,另一个是模型。我的 sql 查询看起来像这样“从 sheet1 中选择不同的模型,其中(Manufacture =@Manufacture)”当我执行它并且如果我要填充数据网格表时,它会起作用。但是,如果我尝试将其放入组合框中,我会得到 System.data.d...... 等供我选择。我怎样才能让它显示值而不是所有这些。我做错了什么?
private void ManuComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
string manu = comboBox3.Text;
string conStr = "Data Source=CA-INVDEV\\RISEDB01;Initial Catalog=RISEDB01; Integrated Security=True";
string sqlcmd = "SELECT DISTINCT Model FROM Sheet1 WHERE (Manufacture =@Manufacture)";
using (SqlConnection conn = new SqlConnection(conStr))
{
SqlCommand cmd = new SqlCommand(sqlcmd, conn);
cmd.Parameters.AddWithValue("@Manufacture", manu);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
dr.Close();
SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
dataAdapter.Fill(table);
bindingSource3.DataSource = table;
ModelComboBox.DataSource = bindingSource3;
}
}
}
【问题讨论】:
-
你试过设置
DisplayMember吗? -
我没有。会试试的。
-
我运气不好。你会建议我如何接近?
-
conn.Open();和 conn.Close();不需要,因为你已经使用了。
-
感谢肖恩。我还是鱿鱼:)
标签: c# visual-studio-2012 .net-4.5 sqldatasource bindingsource