c# - Error after setting different datasources for datagridview combobox cells -
i trying display in datagridview
control names , fields of database tables. in every row name of table displayed in first cell, , second column allows choose of fields of table.
to that, handling 'datagridview.cellbeginedit' event , fill datasource cell names of fields. when trying edit cells, provided list displayed correctly , can b chose fine.
however when try same in row, start getting dataerror
events cell have edited.
event arguments datarow have 'formatting|display' in context
field, , have message "value not allowed in datagridviewcomboboxcell" (or close it). in debug, cell event references has correct value
field, datasource
null , formattedvalue
empty string. , displayed text changes blank.
how should resolved correctly? should derive own custom datagridview cell displays text has combobox editor? if so, how?
edit: here code using:
public class fielddataneededeventargs: eventargs { public list<string> fieldnames { get; private set; } public string tablename { get; private set; } public referencefielddataneededeventargs(stringdata) : base() { fieldnames = new list<string>(); tablename= data; } } ... public event eventhandler<fielddataneededeventargs> fielddataneeded = new eventhandler<fielddataneededeventargs>((sender, args) => { }); ... //handler cellbeginedit //dgvmatch - datagridview, docsreferencetotablematch class datagridview bound private void dgvmatch_cellbeginedit(object sender, datagridviewcellcanceleventargs e) { if (e.columnindex == dgvmatch.columns["tablekey"].index) { docsreferencetotablematch data = dgvmatch.rows[e.rowindex].databounditem docsreferencetotablematch; fielddataneededeventargs ea = new fielddataneededeventargs(data.tablename); fielddataneeded(this, ea); var cell = (dgvmatch.rows[e.rowindex].cells[e.columnindex] datagridviewcomboboxcell); cell.datasource = ea.fieldnames; } } //example handler fielddataneeded event static void ld_referencefielddataneeded(object sender, referencefielddataneededeventargs e) { (int = 0; < 4; i++) { e.fieldnames.add(string.format("{0}_fld{1}", e.referencename, i)); } }