[英]How do i bind data source to a ComboBox?
So i currently have two combo boxes. 所以我目前有两个组合框。 One Being Manufacture the other being model.
一个正在制造另一个是模型。 My sql query looks like this "Select Distinct Model From sheet1 where (Manufacture =@Manufacture)" this works when i execute it and if i were to fill a datagridtable.
我的SQL查询看起来像这样“从Sheet1中选择不同的模型,其中(Manufacture = @ Manufacture)”这在我执行它并且如果我要填充datagridtable时有效。 But if i try to place this into a combobox i get System.data.d...... etc for my selections.
但是如果我尝试将它放入组合框中,我会选择System.data.d ......等等。 How can i just have it show the values instead of all this.
我怎样才能让它显示价值而不是所有这些。 What am i doing wrong?
我究竟做错了什么?
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;
}
}
}
Can you give this a try? 你能尝试一下吗?
using (SqlConnection conn = new SqlConnection(conStr))
{
conn.Open();
SqlCommand cmd = new SqlCommand(sqlcmd, conn);
cmd.Parameters.AddWithValue("@Manufacture", manu);
SqlDataReader dr = cmd.ExecuteReader();
IList<string> modelList = new List<string>()
while (dr.Read())
{
modelList.add(dr[0].ToString());
}
ModelComboBox.DataSource = modelList;
}
If you have set the display member like: 如果您已将显示成员设置为:
ModelComboBox.DataSource = bindingSource3;
ModelComboBox.DisplayMember = "ColumnName";
And it still shows funny values, what are the values that it shows exactly? 它仍然显示有趣的价值,它显示的价值是什么?
Note, in a toolstrip it looks like you may have to also do: 请注意,在工具条中,您可能还需要执行以下操作:
ModelComboBox.BindingContext = this.BindingContext; ModelComboBox.BindingContext = this.BindingContext;
Try adding 尝试添加
ComboBox1.ItemsSource = bindingSource3
if this is your answer then mark it as answer 如果这是你的答案,那么将其标记为答案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.