c# - Update Datagridview From Another Form -
first should saw link :
how refresh datagridview when closing child form?
and did this: (i have datagridview in form1) form1:
public void filldatagridview(datatable dt1) { bindingsource1.datasource = dt1; bindingsource1.resetbindings(false); datagridview1.datasource = bindingsource1; //here checked number of rows of dt1 , shows correct value } form2:
sqlconnection cnn = new sqlconnection(@"my connection string"); private form1 handled_frm1; public form2(form1 frm1) { initializecomponent(); handled_frm1 = frm1; } private void textbox1_textchanged(object sender, eventargs e) { dt.clear(); using (sqlcommand cmd =cnn.createcommand()) { if (cnn.state == connectionstate.closed) { cnn.open(); } cmd.commandtype = commandtype.storedprocedure; cmd.connection = cnn; cmd.commandtext = "spsearchcustomerbyname"; sqlparameter inparam1 = cmd.parameters.addwithvalue("@name", textbox1.text); inparam1.direction = parameterdirection.input; dap.selectcommand = cmd; dap.fill(dt); handled_frm1.filldatagridview(dt); } but value of datagridview not change!
edited:
i wanted test if can clear datagrid view or not,so changed filldatagridview :
public void filldatagridview(datatable dt1) { dt.clear(); datagridview1.columns.clear(); datagridview1.datasource = null; datagridview1.refresh(); } but not clear datagridview1!!!
i used mistakenly incorrect instance of form1!!
in form1,there button when click it,it shows form2.i wrote code in click event of it:
form1 frm1=new form1(); form2 frm2=new form2(frm1); and incorrect,because made additional instance of form1.
and change code this:
form2 frm2=new form2(this);
Comments
Post a Comment