c# - Bindingsource cannot be its own data source -
i receiving error "bindingsource cannot own data source" sporadically when update datagriview bindinglist. there no rhyme or reason far can tell when comes throwing error. if has insight, appreciated.

dgv_items.datasource = null; dgv_items.datasource = new bindingsource(item.objectsitem.orderby(x => x.quality), null); foreach (datagridviewrow row in dgv_items.rows) { try { if (row.cells[5].value != null) { var cell = row.cells[5].value; if (uri.iswellformeduristring(cell.tostring(), urikind.absolute)) { datagridviewlinkcell linkcell = new datagridviewlinkcell { linkcolor = color.blue, value = cell }; row.cells[5] = linkcell; } } } catch (exception ex) { main.log("error: " + ex); } }
the bindingsource meant bind data displayed data. in case displayed data data grid view.
if decide use binding source data source datagridview shouldn't handle datagridview programmatically anymore. because told datagridview datasource bindingsource, program should change data changing data in datasource. once changed, datagridview , bindingsource take care corresponding displayed item updated.
similarly if operator changes displayed item, datagridview , bindingsource take care corresponding object updated.
once datasource of datagridview set, should not change datagridview programmatically, change data in datasource.
you want display uri strings, problem if changes, no 1 gets notified these changes, no 1 can react on changes. reason because class string doesn't implement interface inotifypropertychanged
so have create class of uri strings display.
public class mydisplayableuristring { public string uristring {get; set;} ... // if desired other properties } we want form display list of mydisplayableuristring:
- create new windows forms application
- add bindingsource using toolbox
- bindingsource properties datasource = mydisplayableuristring
- add datagridview using toolbox
- datagridview properties: datasource = bindingsource1
you should see fields of mydisplayableuristring displayed in datagridview.
- create event handler when form loads
in event handler fill bindingsource several persons:
private void form1_load(object sender, eventargs e) { (int = 0; < 10; ++i) { mydisplayableuristring uriitem = new mydisplayableuristring() { uri = "uri " + i.tostring() }; this.bindingsource1.add(uriitem); } } if run program should see table filled created uri items.
now let's change 1 of items:
add button , create handler button click:
private void button1_click(object sender, eventargs e) { mydisplayableuristring uriitem = (mydisplayableuristring)this.bindingsource1[0]; uriitem.uristring = "john"; }
now if run program , click button name not updated.
the reason class doesn't implement inotifypropertychanged. therefore bindingsource doesn't notified when uri item changed. , datagridview not updated.
luckily stackoverflow has article proper implementation: implementing inotifypropertychanged
the code follows:
public event propertychangedeventhandler propertychanged; private string uriname = null; public string uriname { { return this.uriname; } set { this.setfield(ref this.uriname, value); } } protected void setfield<t>(ref t field, t value, [callermembername] string propertyname = null) { if (!equalitycomparer<t>.default.equals(field, value)) { field = value; raiseeventpropertychanged(propertyname); } } private void raiseeventpropertychanged(string propertyname) { var tmpevent = this.propertychanged; if (tmpevent != null) { tmpevent(this, new propertychangedeventargs(propertyname)); } } now whenever program changes object in bindingsource display automatically updated, , if operator changes display items in bindingsource updated, , program notified changes via events
Comments
Post a Comment