笔记# ComboBox 移除指定项

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"] + "";
                }
            }

 

没有更多推荐了,返回首页