带有数据绑定的combox

问题描述:

当我使用databound从数据库中获取值时,如何隐藏此组合中的(或使某些项目不可见).
当我尝试删除某些项目时,发生以下错误
设置DataSource属性时,无法修改项目集合.
最好的问候

when I used databound for get the value from database, how can I hide or (make some items invisable) from this combo.
when I tried to delete some items the following error occur
Items collection cannot be modified when the DataSource property is set.
best regards

首先根据当前ComboBox数据源创建一个DataTable:

first make a DataTable from current ComboBox DataSource:

DataTable dt =(DataTable) comboBox1.DataSource;



然后在DT中找到目标项目并将其从DT中删除.
然后再次为comboBox datasourse设置DT:



then find target item in DT and remove it from DT.
then set again DT for comboBox datasourse:

  DataTable dt = (DataTable)comboBox1.DataSource;
           
DataRow dr = dt.Rows.Find("TargetKey");
dt.Rows.Remove(dr);
           
comboBox1.DataSource = dt;


你好somur-ruteeb,

试试这个..

您没有将集合绑定到combobox数据源,而是为每行添加了项目到combobox.Itemscollection.这样,我们可以为组合框中的每一行使用不同的值.

谢谢

补充
Hello somur-ruteeb,

Try out this..

Instead of binding the the collection to the combobox datasource you''ve added items to combobox.Itemscollection for each row. This way we can have different values for each row in the combobox.

Thanks

Renish