筆記# ComboBox 移除指定項
阿新 • • 發佈:2019-02-18
1、資料來源繫結後,再篩選指定項,要保留value值、text值情況下
//控制元件cmbDJXL if (cmbDJXL.Items.Count > 1) { //得到原先繫結資料來源 DataTable dt = (DataTable)cmbDJXL.DataSource; DataTable dtp = dt.Clone(); DataRow[] rows = dt.Select(" SJDM NOT IN (1,2)"); foreach (DataRow row in rows) { dtp.Rows.Add(row.ItemArray); } //二次篩選得到已排除資料 DataRow dr = dtp.NewRow(); //可插入一行空行 dtp.Rows.InsertAt(dr, 0); cmbDJXL.DataSource = dtp; //繫結Text cmbDJXL.DisplayMember = "SJMC"; //繫結value cmbDJXL.ValueMember = "SJDM"; }
2、移除指定項,只有text值情況下
if (cmbDJXL.Items.Count > 1) { for (int i = 0; i < this.cmbDJXL.Items.Count; i++) { string a = cmbDJXL.Items[i] + ""; if (a.Contains("房")) { cmbDJXL.Items.Remove(a); i--; } } } 補充:獲取指定項值 if (cmbDJXL.Items.Count > 1) { for (int i = 0; i < this.cmbDJXL.Items.Count; i++) { DataRowView tmpRow = (DataRowView)cmbDJXL.Items[i]; string value = tmpRow["SJDM"]+""; string text = tmpRow["SJMC"] + ""; } }