c# - Bind a DataGridViewComboBoxColumn to a DataGridView column -
i struggling few days problem , can not fix myself.
i have 3 datagridviews
- alphabet
- state
- transition
alphabet contains 1 column chars example 'a', 'b' , 'c'
state contains 3 columns: 'name'; 'is start state' , 'is end state'. first string column. other 2 checkbox columns.
transition contains 3 columns. (combobox), (combobox), symbol (combobox)
my goal use data user entered in state & alphabet tables comboboxes.
for example: user entered in alphabet folowing data: 'a' , 'b' user entered in state folowing data:
- lr_0; start state; no end state
- lr_1; no start state; no end state
- lr_2; no start state; end state
when user want fill transitions can choose column 'from' , 'to' de folowing options: lr_0; lr_1 or lr_2. symbol can use 'a' or 'b'.
now comes tricky part. when user rename lr_0 lr_3 comboboxes need update. , when user removes lr_0 row rows in transitions contains lr_0 needs remove.
i've used lot of posibilities none working 100% correct.
option 1 use intern list , bind bind comboboxes , everytime there row added add list. when there row removed remove list , remove every row in transitions contains value.
problems: unknown when there value changed , when value added. difficult change value in comboboxes
option 2 use ilist interface , make enumerator folowing datagridview information .rows.count = length , .rows[0].cells[0].value = value.
problems: when there information changed. combobox isn't updated
option 3 use ibindinglist interface same setup option 2. correct. list changed when remove or add something. problem comboboxes don't change value when specific row changed.
i have @ moment this. datatablecolumnsource
using system; using system.componentmodel; using system.windows.forms; namespace gui { public class datatablecolumnsource<t> : ibindinglist { public event listchangedeventhandler listchanged; public datagridview datatable { get; set; } public int column { get; set; } public datatablecolumnsource(ref datagridview datatable, int column) { this.datatable = datatable; this.column = column; } public void posiblechange(listchangedtype type, int index) { if (listchanged != null) { listchanged(this, new listchangedeventargs(type, index)); } } public void addindex(propertydescriptor property) { throw new notimplementedexception(); } public object addnew() { return new comboboxitem<t>(default(t), -1); ; } public bool allowedit { { throw new notimplementedexception(); } } public bool allownew { { return true; } } public bool allowremove { { throw new notimplementedexception(); } } public void applysort(propertydescriptor property, listsortdirection direction) { throw new notimplementedexception(); } public int find(propertydescriptor property, object key) { throw new notimplementedexception(); } public bool issorted { { throw new notimplementedexception(); } } public void removeindex(propertydescriptor property) { throw new notimplementedexception(); } public void removesort() { throw new notimplementedexception(); } public listsortdirection sortdirection { { throw new notimplementedexception(); } } public propertydescriptor sortproperty { { throw new notimplementedexception(); } } public bool supportschangenotification { { return true; } } public bool supportssearching { { return false; } } public bool supportssorting { { return false; } } public int add(object value) { throw new notimplementedexception(); } public void clear() { throw new notimplementedexception(); } public bool contains(object value) { throw new notimplementedexception(); } public int indexof(object value) { throw new notimplementedexception(); } public void insert(int index, object value) { throw new notimplementedexception(); } public bool isfixedsize { { throw new notimplementedexception(); } } public bool isreadonly { { throw new notimplementedexception(); } } public void remove(object value) { throw new notimplementedexception(); } public void removeat(int index) { throw new notimplementedexception(); } public object this[int index] { { if (this.datatable.rows[index].cells[this.column].value == null) return null; t w =(t) convert.changetype(this.datatable.rows[index].cells[this.column].value, typeof(t)); return w; } set { throw new notimplementedexception(); } } public void copyto(array array, int index) { throw new notimplementedexception(); } public int count { { return this.datatable.rows.count -1; } } public bool issynchronized { { throw new notimplementedexception(); } } public object syncroot { { throw new notimplementedexception(); } } public system.collections.ienumerator getenumerator() { int c = count; (int = 0; < c; i++) yield return this[i]; } } }
gui
public partial class frmndfa : form { datatablecolumnsource<char> alphabetsource; datatablecolumnsource<string> statesource; int lastdeletedindex; public frmndfa() { initializecomponent(); alphabetsource = new datatablecolumnsource<char>(ref this.dgvalphabet, 0); statesource = new datatablecolumnsource<string>(ref this.dgvstates, 0); datagridviewcomboboxcolumn column = (datagridviewcomboboxcolumn)this.dgvtransitions.columns[0]; bindingsource clm1bs = new bindingsource(); clm1bs.datasource = statesource; column.datasource = clm1bs; column.valuetype = typeof(string); column = (datagridviewcomboboxcolumn)this.dgvtransitions.columns[1]; bindingsource clm2bs = new bindingsource(); clm2bs.datasource = statesource; column.datasource = clm2bs; column.valuetype = typeof(string); column = (datagridviewcomboboxcolumn)this.dgvtransitions.columns[2]; bindingsource clm3bs = new bindingsource(); clm3bs.datasource = alphabetsource; column.datasource = clm3bs; column.valuetype = typeof(char); } private void dgvalphabet_userdeletingrow(object sender, datagridviewrowcanceleventargs e) { lastdeletedindex = e.row.index; } private void dgvalphabet_userdeletedrow(object sender, datagridviewroweventargs e) { datagridviewrow row = e.row; string value = (row.cells[0].value != null)? row.cells[0].value.tostring() : ""; alphabetsource.posiblechange(listchangedtype.itemdeleted, lastdeletedindex); list<datagridviewrow> removerows = new list<datagridviewrow>(); //update transitions foreach (datagridviewrow datarow in this.dgvtransitions.rows) { if (datarow.cells[2].value != null && datarow.cells[2].value.tostring().equals(value)) removerows.add(datarow); } foreach(datagridviewrow datarow in removerows) { this.dgvtransitions.rows.remove(datarow); } } private void dgvalphabet_cellendedit(object sender, datagridviewcelleventargs e) { if(this.dgvalphabet.rows[e.rowindex].isnewrow) alphabetsource.posiblechange(listchangedtype.itemadded, e.rowindex); else alphabetsource.posiblechange(listchangedtype.itemchanged, e.rowindex); } private void dgvalphabet_cellvalidating(object sender, datagridviewcellvalidatingeventargs e) { e.cancel = e.formattedvalue != null && e.formattedvalue.tostring().length > 1; } private void dgvstates_userdeletingrow(object sender, datagridviewrowcanceleventargs e) { lastdeletedindex = e.row.index; } private void dgvstates_userdeletedrow(object sender, datagridviewroweventargs e) { datagridviewrow row = e.row; string value = (row.cells[0].value != null) ? row.cells[0].value.tostring() : ""; statesource.posiblechange(listchangedtype.itemdeleted, this.dgvstates.rows.indexof(e.row)); list<datagridviewrow> removerows = new list<datagridviewrow>(); //update transitions foreach (datagridviewrow datarow in this.dgvtransitions.rows) { if (datarow.cells[0].value != null && datarow.cells[0].value.equals(value)) removerows.add(datarow); else if (datarow.cells[1].value != null && datarow.cells[1].value.equals(value)) removerows.add(datarow); } foreach(datagridviewrow datarow in removerows) { this.dgvtransitions.rows.remove(datarow); } } private void dgvstates_cellendedit(object sender, datagridviewcelleventargs e) { if (e.columnindex != 0) return; statesource.posiblechange(listchangedtype.itemchanged, e.rowindex); } }
Comments
Post a Comment