[英]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.