Update DataGridView on button Press -
this first windows form/sql application. how update table changes database on submit button press?
when trying add 'dt' datatable doesn't recognize it, can't figure out how put button function inside try/catch statement.
obviously in 2 functions now, how make work 1 function?
code below.
using system; using system.collections.generic; using system.componentmodel; using system.drawing; using system.linq; using system.text; using system.windows.forms; using system.data.sql; using system.data; using system.data.sqlclient; namespace accountsapp { public partial class search : form { public search(string searchstring, string searchtype) { initializecomponent(); try { if (searchstring != null) { string strcon = "data source=omiw2310.orthman.local;initial catalog=accts;integrated security=true"; string strsql = "select dbo.account.username, dbo.account.password, dbo.account.url, dbo.host.hostname, dbo.service.servicename, dbo.servicetype.svctypename dbo.account inner join dbo.host on dbo.account.accounthost = dbo.host.hostid inner join dbo.service on dbo.account.accountservice = dbo.service.serviceid inner join dbo.servicetype on dbo.account.accounttype = dbo.servicetype.svctypeid " + searchtype + "= '" + searchstring + "'"; sqldataadapter dataadapter = new sqldataadapter(strsql, strcon); sqlcommandbuilder commandbuilder = new sqlcommandbuilder(dataadapter); datatable dt = new datatable(); dataadapter.fill(dt); dbgridview.autoresizecolumns(datagridviewautosizecolumnsmode.allcellsexceptheader); dbgridview.datasource = dt; textbox1.text = searchstring; textbox2.text = searchtype; } } catch (sqlexception ex){ messagebox.show(ex.message); } } private void label1_click(object sender, eventargs e) { } private void textbox1_textchanged(object sender, eventargs e) { } private void search_load(object sender, eventargs e) { } private void textbox2_textchanged(object sender, eventargs e) { } private void button2_click(object sender, eventargs e) { application.exit(); } private void cancelbutton_click(object sender, eventargs e) { this.close(); } private void button1_click(object sender, eventargs e) { dbgridview.update(); } } }
from understand, want update database using submit button (which presume button1). below changes have made form explanations in code comments:
using system; using system.collections.generic; using system.componentmodel; using system.drawing; using system.linq; using system.text; using system.windows.forms; using system.data.sql; using system.data; using system.data.sqlclient; namespace accountsapp { public partial class search : form { //make dataadapter , datatable members can //be used submit button method private sqldataadapter dataadapter; private datatable dt; public search(string searchstring, string searchtype) { initializecomponent(); try { if (searchstring != null) { string strcon = "data source=omiw2310.orthman.local;initial catalog=accts;integrated security=true"; string strsql = "select dbo.account.username, dbo.account.password, dbo.account.url, dbo.host.hostname, dbo.service.servicename, dbo.servicetype.svctypename dbo.account inner join dbo.host on dbo.account.accounthost = dbo.host.hostid inner join dbo.service on dbo.account.accountservice = dbo.service.serviceid inner join dbo.servicetype on dbo.account.accounttype = dbo.servicetype.svctypeid " + searchtype + "= '" + searchstring + "'"; dataadapter = new sqldataadapter(strsql, strcon); sqlcommandbuilder commandbuilder = new sqlcommandbuilder(dataadapter); dt = new datatable(); dataadapter.fill(dt); dbgridview.autoresizecolumns(datagridviewautosizecolumnsmode.allcellsexceptheader); dbgridview.datasource = dt; textbox1.text = searchstring; textbox2.text = searchtype; } } catch (sqlexception ex){ messagebox.show(ex.message); } } private void label1_click(object sender, eventargs e) { } private void textbox1_textchanged(object sender, eventargs e) { } private void search_load(object sender, eventargs e) { } private void textbox2_textchanged(object sender, eventargs e) { } private void button2_click(object sender, eventargs e) { application.exit(); } private void cancelbutton_click(object sender, eventargs e) { this.close(); } private void button1_click(object sender, eventargs e) { //dbgridview.update(); <------ redraws datagridview. //has nothing data. //code required update (for submit work) //make sure datagriview has ended editing datagridview.endedit(); //call update on datatable bound datagrdiview dataadapter.update(dt); } } }
Comments
Post a Comment