c# - How to have custom control in DataGridView display object's value? -
i have sample project located here.
the project has main form form1
user can enter customers in datagridview. customertype
column custom control , when user clicks button, search form form2
pops up.
the search form populated list of type customertype
. user can select record double-clicking on row, , object should set in custom control. datagridview
should display description
property in background each cell should hold value (ie. customertype
instance).
the relevant code located in following classes:
the column class:
public class datagridviewcustomertypecolumn : datagridviewcolumn { public datagridviewcustomertypecolumn() : base(new customertypecell()) { } public override datagridviewcell celltemplate { { return base.celltemplate; } set { if (value != null && !value.gettype().isassignablefrom(typeof(customertypecell))) { throw new invalidcastexception("should customertypecell."); } base.celltemplate = value; } } }
the cell class:
public class customertypecell : datagridviewtextboxcell { public customertypecell() : base() { } public override void initializeeditingcontrol(int rowindex, object initialformattedvalue, datagridviewcellstyle datagridviewcellstyle) { base.initializeeditingcontrol(rowindex, initialformattedvalue, datagridviewcellstyle); customertypesearch ctl = datagridview.editingcontrol customertypesearch; if (this.value == null) ctl.value = (customertype)this.defaultnewrowvalue; else ctl.value = (customertype)this.value; } public override type edittype { { return typeof(customertypesearch); } } public override type valuetype { { return typeof(customertype); } } public override object defaultnewrowvalue { { return null; } } }
and custom control:
public partial class customertypesearch : usercontrol, idatagridvieweditingcontrol { private datagridview datagridview; private int rowindex; private bool valuechanged = false; private customertype value; public customertypesearch() { initializecomponent(); } public customertype value { { return this.value; } set { this.value = value; if (value != null) textboxsearch.text = value.description; else textboxsearch.clear(); } } private void buttonsearch_click(object sender, eventargs e) { form2 f = new form2(); dialogresult dr = f.showdialog(this); if (dr == dialogresult.ok) { value = f.selectedvalue; } } #region idatagridvieweditingcontrol implementation public object editingcontrolformattedvalue { { if (this.value != null) return this.value.description; else return null; } set { if (this.value != null) this.value.description = (string)value; } } public object geteditingcontrolformattedvalue(datagridviewdataerrorcontexts context) { return editingcontrolformattedvalue; } public void applycellstyletoeditingcontrol(datagridviewcellstyle datagridviewcellstyle) { this.borderstyle = borderstyle.none; this.font = datagridviewcellstyle.font; } public int editingcontrolrowindex { { return rowindex; } set { rowindex = value; } } public bool editingcontrolwantsinputkey(keys key, bool datagridviewwantsinputkey) { return false; } public void prepareeditingcontrolforedit(bool selectall) { //no preparation needs done } public bool repositioneditingcontrolonvaluechange { { return false; } } public datagridview editingcontroldatagridview { { return datagridview; } set { datagridview = value; } } public bool editingcontrolvaluechanged { { return valuechanged; } set { valuechanged = value; } } public cursor editingpanelcursor { { return base.cursor; } } #endregion private void customertypesearch_resize(object sender, eventargs e) { buttonsearch.left = this.width - buttonsearch.width; textboxsearch.width = buttonsearch.left; } }
however, datagridview
not displaying text , not keeping customertype
value each cell.
what missing?
first, override tostring()
method customertype
class.
second, avoid parsing errors, modify following method in customertypesearch
class :
public customertype value { { return this.value; } set { this.value = value; if (value != null) textboxsearch.text = value.description; else textboxsearch.clear(); valuechanged = true; if ((editingcontroldatagridview.currentcell customertypecell) != null) (editingcontroldatagridview.currentcell customertypecell).value = value; } }
then, add function custormertypecell
class :
protected override bool setvalue(int rowindex, object value) { if (value != null) return base.setvalue(rowindex, value); return false; }
finally, add handle data errors :
private void datagridview_dataerror(object sender, datagridviewdataerroreventargs e) { e.cancel = false; }
Comments
Post a Comment