[英]Combobox set selected value from SQL
我在數據庫中有兩個表,即Notes
和Categories
(其中Notes
具有指向Categories
的外鍵)。
我已經從表Notes
加載了數據,並在表類別中的組合框中加載了數據。
現在,我需要選擇與組合框中的注釋相同的類別(外鍵)。
我的代碼是:
DataTable dtCat = qc.getCategory();
cmbCategory.DataSource = dtCat;
cmbCategory.DisplayMember = "categoryName";
cmbCategory.ValueMember = "categoryId";
cmbCategory.SelectedValue = "categoryId";
嘗試這個:
DataTable dt = new DataTable();
dt.Columns.Add("categoryName");
dt.Columns.Add("categoryId");
dt.Rows.Add(new object[] {"Cat1", "1"});
dt.Rows.Add(new object[] {"Cat2", "2"});
dt.Rows.Add(new object[] {"Cat3", "3"});
this.comboBox1.DisplayMember = "categoryName";
this.comboBox1.ValueMember = "categoryId";
this.comboBox1.DataSource = dt;
this.comboBox1.SelectedValue = dt.Rows[1]["categoryId"]; // select Cat2
因此,您可以通過DataTable的行值中的categoryId列指定所選值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.