c# - Associate two classes so a combobox appears with the second class -
i have winforms application. there 2 classes. 1 class stores people contribute book. other class list of possible contribution types (like editor, reviewer, , on).
the bookcontributors class datasource datagridview , works fine. now, want have bookcontributortypes class combobox feeds bookcontributors class.
so far in reading, looks have number of options, including forcing combobox datagridview, using class attributes, creating 1-to-many relationship between classes.
since code have many of these types of situations, want right. think means showing relationship between 2 classes somehow datagridview knows display combobox, not sure how go doing this.
bookcontributors populated person filling out contributor. example: bookcontributor = "john smith"; bookcontributorfileas="smith, john"; bookcontributortype = "rev".
public class bookcontributors : inotifypropertychanged { private string _bookcontributor; [descriptionlocalized(typeof(resourcesclassbooks), "bookcontributorcomment")] [displaynamelocalized(typeof(resourcesclassbooks), "bookcontributordisplayname")] public string bookcontributor { { return _bookcontributor; } set { if (setfield(ref _bookcontributor, value, "bookcontributor")) { // when user types author name, add sorted name sorted field. // ex name = william e raymond // ex file = raymond, william e var name = _bookcontributor.split(' '); if (name.length >= 2) { string fileasname = (name[name.length - 1] + ","); (int = 0; <= (name.length - 2); i++) { fileasname = fileasname + " " + name[i]; } bookcontributorfileas = fileasname; } } } } private string _bookcontributorfileas; [descriptionlocalized(typeof(resourcesclassbooks), "bookcontributorfileascomment")] [displaynamelocalized(typeof(resourcesclassbooks), "bookcontributorfileasdisplayname")] public string bookcontributorfileas { { return _bookcontributorfileas; } set { setfield(ref _bookcontributorfileas, value, "bookcontributorfileas"); } } private string _bookcontributortype; [descriptionlocalized(typeof(resourcesclassbooks), "bookcontributortypecomment")] [displaynamelocalized(typeof(resourcesclassbooks), "bookcontributortypedisplayname")] public string bookcontributortype { { return _bookcontributortype; } set { setfield(ref _bookcontributortype, value, "bookcontributortype"); } } #region handle property changes public event propertychangedeventhandler propertychanged; protected bool setfield<t>(ref t field, t value, string propertyname) { //if value did not change, nothing. if (equalitycomparer<t>.default.equals(field, value)) return false; //the value did change, make modification. field = value; return true; } protected virtual void onpropertychanged(string propertyname) { propertychangedeventhandler handler = propertychanged; if (handler != null) handler(this, new propertychangedeventargs(propertyname)); } #endregion }
bookcontributortypes (populated list of bookcontributor types xml file). example book contributor type id's: "rev", "edt". example book contributor type descriptions: "reviewer", "editor".
class bookcontributortypes : inotifypropertychanged { private string _bookcontributortypeid; [descriptionlocalized(typeof(resourcesclassbooks), "bookcontributortypeidcomment_lkp")] [displaynamelocalized(typeof(resourcesclassbooks), "bookcontributortypeiddisplayname_lkp")] public string bookcontributortypeid { { return _bookcontributortypeid; } set { setfield(ref _bookcontributortypeid, value, "bookcontributortypeid"); } } private string _bookcontributortypedescription; [descriptionlocalized(typeof(resourcesclassbooks), "bookcontributortypecomment_lkp")] [displaynamelocalized(typeof(resourcesclassbooks), "bookcontributortypedisplayname_lkp")] public string bookcontributortypedescription { { return _bookcontributortypedescription; } set { setfield(ref _bookcontributortypedescription, value, "bookcontributortypedescription"); } } #region handle property changes public event propertychangedeventhandler propertychanged; protected bool setfield<t>(ref t field, t value, string propertyname) { //if value did not change, nothing. if (equalitycomparer<t>.default.equals(field, value)) return false; //the value did change, make modification. field = value; return true; } protected virtual void onpropertychanged(string propertyname) { propertychangedeventhandler handler = propertychanged; if (handler != null) handler(this, new propertychangedeventargs(propertyname)); } #endregion }
all have working right now, without combo box, because not know how implement combobox show selection options bookcontributortypes class:
datagridview1.datasource = bookcontributors;
thanks ay can provide.
this ended being lot more difficult research thought. turns out, if want place column in datagridview combobox , have combobox interact class, not quick one-liner.
my example has 2 classes:
bookcontributors
(bookcontributors
) - list of people have contributed book. example, stores person's name , type of contribution made.bookcontributortypes
(bookcontributortypes
) - list of possible types of contributions book, editor or contributor.- i want
contributortypes
class combobox , store user's selectionbookcontributors
class,bookcontributortype
property.
here code came with, along additional notes found along way. best of knowledge, information provide accurate :-)
gvcontributors.datasource = bookcontributors; //set datgridview's datasource displays class want. gvcontributors.columns["bookcontributortype"].visible = false; //hide column (property) want replace combobox. datagridviewcomboboxcolumn contribtype = new datagridviewcomboboxcolumn(); //create combobox object. contribtype.headertext = "my column"; //the text display in column header. contribtype.name = "bookcontributortype"; //name of class property set visible=false. note sure if needed. contribtype.datasource = bookcontributortypes; //name of class provides combobox data. contribtype.displaymember = "bookcontributortypedescription"; //data user see when clicking combobox. contribtype.valuemember = "bookcontributortypeid"; //data store in class property. contribtype.datapropertyname = "bookcontributortype"; //the class property binding in order store data. gvcontributors.columns.add(contribtype); //add new combobox datagridview.
what have following:
- a
datagridview
datasource. in case, datasource class. - a hidden (
visible=false
) column want make combobox. - a
datagridviewcombobox
links datasource user see list of values. usingdatapropertyname
same name hidden column, binding datagridview'sdatasource
.
another answer on post suggests remove
column replacing combobox visible=false
seems work me.
when run solution, find user has click twice on combobox. have not tried code yet, think post resolve problem: open dropdown(in datagrid view) items on single click
Comments
Post a Comment