c# - DGV updates only when cell is clicked -
i have wfa, @ moment has 2 forms.
the first form contains datagridview, it's data source being list. when user clicks button, opens form2. within form2 have bubblesort algorithm sorts list 1 of it's properties. passes updates list form1, , sets new datasource bubbledsorted updated list form2.
now works, bubblesort works. however, dgv on form1 doesn't update unless click on individual rows/cells of dgv. have click on each 1 individually them in new sorted positions.
here's code have:
form1:
//this shows form2 private void sortbypriority_click(object sender, eventargs e) { fm2 = new form2(); fm2.show(); } //this called form2 set new datasource public void refreshdatagrid(datagridview p) { datagridview1.datasource = null; datagridview1.datasource = p.datasource; }
form2:
private void sortpriority_click(object sender, eventargs e) { int temp = 0; bool tempforcomp; string tempfordate = ""; string tempfordesc = ""; string tempforname = ""; (int write = 0; write < 10; write++) { (int sort = 0; sort < todogen.task.count - 1; sort++) { if (todogen.task[sort].priority > todogen.task[sort + 1].priority) { temp = todogen.task[sort + 1].priority; tempforcomp = todogen.task[sort + 1].completed; tempfordate = todogen.task[sort + 1].dateofcompletion; tempfordesc = todogen.task[sort + 1].description; tempforname = todogen.task[sort + 1].name; todogen.task[sort + 1].priority = todogen.task[sort].priority; todogen.task[sort + 1].completed = todogen.task[sort].completed; todogen.task[sort + 1].dateofcompletion = todogen.task[sort].dateofcompletion; todogen.task[sort + 1].description = todogen.task[sort].description; todogen.task[sort + 1].name = todogen.task[sort].name; todogen.task[sort].priority = temp; todogen.task[sort].completed = tempforcomp; todogen.task[sort].dateofcompletion = tempfordate; todogen.task[sort].description = tempfordesc; todogen.task[sort].name = tempforname; } } datagrid n = new datagrid(); n.datasource = todogen.task; refresh(); } } private void refresh() { fm1 = new form1(); fm1.refreshdatagrid(n); }
edit: updated , clear - need use multiple forms (university assignment), , nonetheless, didn't update when had sorting algorithm in form1.
i'll make simplified example doesn't use bubblesort algorithm, hope helps:
this code of form1 class. define stringvalue class because of this: how bind list<string> datagridview control?
public class stringvalue { public stringvalue(string s) { _value = s; } public string value { { return _value; } set { _value = value; } } string _value; } public partial class form1 : form { public form1() { initializecomponent(); } private void form1_load(object sender, eventargs e) { datagridview1.datasource = new list<stringvalue> { new stringvalue("string1"), new stringvalue("string3"), new stringvalue("string2") }; } private void button1_click(object sender, eventargs e) { form2 form2 = new form2(this, datagridview1.datasource list<stringvalue>); form2.show(); } public void refreshgrid(list<stringvalue> source) { datagridview1.datasource = source; } }
the button click event opens form2 , passes grid view datasource. here's form2:
public partial class form2 : form { form1 _form1; list<stringvalue> _source; public form2(form1 form1, list<stringvalue> source) { _form1=form1; _source=source; initializecomponent(); } private void button1_click(object sender, eventargs e) { _source = _source.orderby(x => x.value).tolist(); _form1.refreshgrid(_source); this.close(); } }
after click on button in form2 datagridview1's data source refreshed sorted version, , form2 closed.
Comments
Post a Comment