datagridview - Update and refresh datagrid view -
i have info entry form datagridview, utilize local database.sdf file. when search particular name of person, click edit button new update form displayed. info succesfully passed update form
requirement: succesfully update info of person update form , info refreshed , shown in datagridview when nail update button , update form closes
error: first tried sqlcedataadapter adap.fill(databasedataset, "mainform"); //rest of code shown down// when click update button updates info creates entire re-create of whole info nowadays in datagridview. tried sqlcedataadapter adap.update(databasedataset, "mainform"); , code updates info succesfully , not create copies datagridview not refresh, info shown when close , open application again
code tried: in form 1:
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.windows.forms; using system.data.sqlclient; using system.data.sqlserverce; namespace hdr2 { public partial class frmupdate : form { public string name, age, gender, family_member, family_member_name, shnong, dong, grand, amount, phone_number, date_registered, date_recieved, status; private form1 f1; public frmupdate(form1 f2) { initializecomponent(); f1 = f2; } public void passdgvvaluetoform2() { nametxtupdate.text = name; agetxtupdate.text = age; gendertxtupdate.text = gender; familymembertxtupdate.text = family_member; familymembernametxtupdate.text = family_member_name; shnongtxtupdate.text = shnong; dongtxtupdate.text = dong; grandtxtupdate.text = grand; amounttxtupdate.text = amount; phonenumbertxtupdate.text = phone_number; dateregisteredupdate.text = date_registered; daterecievedupdate.text = date_recieved; statuscbupdate.text = status; } private void frmupdate_load(object sender, eventargs e) { } private void update() { sqlceconnection con = new sqlceconnection("data source=database.sdf"); con.open(); using (sqlcecommand cmd = new sqlcecommand("update mainform set name=@name, age=@age, gender=@gender, family_member=@family_member, family_member_name=@family_member_name, shnong=@shnong, dong=@dong, grand=@grand, amount=@amount, phone_number=@phone_number, date_registered=@date_registered, date_recieved=@date_recieved, status=@status name=@name", con)) { cmd.parameters.addwithvalue("@name", nametxtupdate.text); cmd.parameters.addwithvalue("@age", agetxtupdate.text); cmd.parameters.addwithvalue("@gender", gendertxtupdate.text); cmd.parameters.addwithvalue("@family_member", familymembertxtupdate.text); cmd.parameters.addwithvalue("@family_member_name", familymembernametxtupdate.text); cmd.parameters.addwithvalue("@shnong", shnongtxtupdate.text); cmd.parameters.addwithvalue("@dong", dongtxtupdate.text); cmd.parameters.addwithvalue("@grand", grandtxtupdate.text); cmd.parameters.addwithvalue("@amount", amounttxtupdate.text); cmd.parameters.addwithvalue("@phone_number", phonenumbertxtupdate.text); cmd.parameters.addwithvalue("@date_registered", dateregisteredupdate.text); cmd.parameters.addwithvalue("@date_recieved", daterecievedupdate.text); cmd.parameters.addwithvalue("@status", statuscbupdate.text); cmd.executenonquery(); con.close(); } con.close(); } private void frmupdate_formclosed(object sender, formclosedeventargs e) { f1.loademployee(); } private void btnupdate_click_1(object sender, eventargs e) { update(); f1.loademployee(); this.close(); } } }
and in form 2 //frmupdate:
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.windows.forms; using system.data.sqlclient; using system.data.sqlserverce; namespace hdr2 { public partial class frmupdate : form { public string name, age, gender, family_member, family_member_name, shnong, dong, grand, amount, phone_number, date_registered, date_recieved, status; private form1 f1; public frmupdate(form1 f2) { initializecomponent(); f1 = f2; } public void passdgvvaluetoform2() { nametxtupdate.text = name; agetxtupdate.text = age; gendertxtupdate.text = gender; familymembertxtupdate.text = family_member; familymembernametxtupdate.text = family_member_name; shnongtxtupdate.text = shnong; dongtxtupdate.text = dong; grandtxtupdate.text = grand; amounttxtupdate.text = amount; phonenumbertxtupdate.text = phone_number; dateregisteredupdate.text = date_registered; daterecievedupdate.text = date_recieved; statuscbupdate.text = status; } private void frmupdate_load(object sender, eventargs e) { } private void update() { sqlceconnection con = new sqlceconnection("data source=database.sdf"); con.open(); using (sqlcecommand cmd = new sqlcecommand("update mainform set name=@name, age=@age, gender=@gender, family_member=@family_member, family_member_name=@family_member_name, shnong=@shnong, dong=@dong, grand=@grand, amount=@amount, phone_number=@phone_number, date_registered=@date_registered, date_recieved=@date_recieved, status=@status name=@name", con)) { cmd.parameters.addwithvalue("@name", nametxtupdate.text); cmd.parameters.addwithvalue("@age", agetxtupdate.text); cmd.parameters.addwithvalue("@gender", gendertxtupdate.text); cmd.parameters.addwithvalue("@family_member", familymembertxtupdate.text); cmd.parameters.addwithvalue("@family_member_name", familymembernametxtupdate.text); cmd.parameters.addwithvalue("@shnong", shnongtxtupdate.text); cmd.parameters.addwithvalue("@dong", dongtxtupdate.text); cmd.parameters.addwithvalue("@grand", grandtxtupdate.text); cmd.parameters.addwithvalue("@amount", amounttxtupdate.text); cmd.parameters.addwithvalue("@phone_number", phonenumbertxtupdate.text); cmd.parameters.addwithvalue("@date_registered", dateregisteredupdate.text); cmd.parameters.addwithvalue("@date_recieved", daterecievedupdate.text); cmd.parameters.addwithvalue("@status", statuscbupdate.text); cmd.executenonquery(); con.close(); } con.close(); } private void frmupdate_formclosed(object sender, formclosedeventargs e) { f1.loademployee(); } private void btnupdate_click_1(object sender, eventargs e) { update(); f1.loademployee(); this.close(); } } }
// note tried adap.fill not work.
datagridview
No comments:
Post a Comment