This article is reproduced in: http://hi.baidu.com/yangyangye2008/blog/item/1775770215a22782d53f7c18.html
Similar to the two, we know that after the data binding control is bound, we can no longer add data to it, because this is the post-datasource that cannot be modified! Whether you use the add method under items before the ComboBox operation, it will be overwritten by the displaymember,
① This is not acceptable:
Combobox1.items. Add ("-- select all --");
This. combobox1.datasource = employerset;
This. combobox1.displaymember = "maid. firstname ";
If this type of error occurs, you must add one that is unknown and will be overwritten later!
② Some may think about the following:
This. combobox1.datasource = employerset;
This. combobox1.displaymember = "maid. firstname ";
Combobox1.items. insert (0, "-- select all --");
This is the most common error. At this time, the error will appear in the red position: the item set cannot be modified after the datasource attribute is set.
In my long-term experience, I found that there are still feasible methods. The second is what I found during my project yesterday:
The first method is to modify the bound data source before binding. The Code is as follows:
Datatable dt = employerset. Tables ["employeestable"];
Datarow DR = DT. newrow ();
Dr ["firstname"] = "-- select all --";
DT. Rows. insertat (DR, 0 );
This. combobox1.datasource = DT;
This. combobox1.displaymember = "firstname ";
Method 2:
The data source management object bindingcontext is used. The current item value is modified before being assigned to the rendering member.
This. combobox1.datasource = employerset;
Datarowview rowv = (datarowview) This. bindingcontext [employerset, "employeestable"]. Current;
Rowv ["firstname"] = "-- select all --";
This. combobox1.displaymember = "maid. firstname ";
You can test and find that the original value of rowv ["firstname"] Is employeestable. the first value in firstname is modified later and then the rendering member is assigned. The reason why the data source is successfully modified is that the data management object is used, it's like the administrator can modify it, but others can't modify it!