[英]Adding Items to ListBox, RadioList, Combobox using reflection
[英]Using combobox(using databound items) to populate listbox
如何使用选择查询(源自组合框,数据绑定项)填充列表框
如果在组合框中选择了“多冰淇淋”,那么我希望系统选择该行并向我显示列表框中“过程”列中的内容
目前,该软件正在返回该列下的所有行
MAcon.Open();
OleDbDataAdapter da = new OleDbDataAdapter("Select * from [Product Family] Where Process = @Process", MAcon);
da.SelectCommand.Parameters.AddWithValue("@Process", ItemCBx.Text);
DataTable dtbl = new DataTable();
da.Fill(dtbl);
if (dtbl.Rows.Count == 1)
{
Listbox1.Text.ToString().Split(',').ToList().ForEach(c => Listbox1.Items.Add(c.Trim()));
Define.SelectedIndex = 1;
}
MAcon.Close();
您需要为用户在comboBox中选择某些内容时添加一个eventHandler。
然后,获取诸如comboBox1.SelectedItem.ToString()
类的selectedItem并将其传递给查询。
da.SelectCommand.Parameters.AddWithValue("@Process",comboBox1.SelectedItem.ToString());
然后
if (dtbl.Rows.Count == 1)
{
// Selects the value located in teh first row and first columns
// if that is what your query is returning
listBox1.Items.Add(dtbl.Rows[0][0]);
}
问题:我不知道您的查询返回什么,看到*
表示它将返回多列,并且列表框中每行只能包含一个元素,我建议您使用dataGridView控件和dataGridView1.DataSource = dt;
它将显示所有返回的列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.