c# - updating DATAGRID row to save to SQL -
hi guys trying understand how save , edited row db
private void budgetgrid_roweditending(object sender, datagridroweditendingeventargs e) { sqlcommand gridcmd = new sqlcommand(); sqlconnection rwconn = null; rwconn = new sqlconnection("server=localhost;" + "trusted_connection=yes;" + "database=production; " + "connection timeout=30"); gridcmd.connection = rwconn; rwconn.open(); //gridcmd.commandtext = "select id, name, quantity, rate, time budget"; gridcmd.commandtext = "update budget set id = @id, name = @name, quantity = @qty, rate = @rte time = @time"; sqldataadapter gridda = new sqldataadapter(gridcmd); string strid = "@id".tostring(); int intid; bool bintid = int32.tryparse(strid, out intid); string strname = "@name".tostring(); string strqty = "@qty".tostring(); int intqty; bool bintqty = int32.tryparse(strqty, out intqty); string strrte = "@rte".tostring(); int intrte; bool bintrte = int32.tryparse(strrte, out intrte); string strtime = "@time".tostring(); gridda.selectcommand.parameters.add(new sqlparameter("@id", sqldbtype.int)); gridda.selectcommand.parameters["@id"].sqlvalue = intid; gridda.selectcommand.parameters.add(new sqlparameter("@name", sqldbtype.varchar)); gridda.selectcommand.parameters["@name"].sqlvalue = strname; gridda.selectcommand.parameters.add(new sqlparameter("@qty", sqldbtype.int)); gridda.selectcommand.parameters["@qty"].sqlvalue = strqty; gridda.selectcommand.parameters.add(new sqlparameter("@rte", sqldbtype.int)); gridda.selectcommand.parameters["@rte"].sqlvalue = strrte; gridda.selectcommand.parameters.add(new sqlparameter("@time", sqldbtype.varchar)); gridda.selectcommand.parameters["@time"].sqlvalue = strtime; datatable griddt = new datatable("budget"); gridda.fill(griddt); gridda.updatecommand = new sqlcommandbuilder(gridda).getupdatecommand(); budgetgrid.itemssource = griddt.defaultview; gridda.update(griddt); rwconn.close(); }
it displays fine. can edit when click on other tab not update goes original data.
most of code have been going through either out dated.. or not looking for.
so if hit tab next row. under event budgetgrid_roweditending
should update database.. not.
just copy below codes. i've created thing of , tested successfully. rather first way, tried let go more popular way. therefore, took me time adopt..
hope helps !
sqldataadapter da; datatable dt; private void window_loaded(object sender, routedeventargs e) { sqlconnection conn = new sqlconnection(); conn.connectionstring = yourconnectionstring; conn.open(); sqlcommand gridcomm = new sqlcommand(); gridcomm.connection = conn; gridcomm.commandtext = "select id, name, quantity, rate, time budget"; da = new sqldataadapter(gridcomm); sqldatareader gridreader = gridcomm.executereader(); while (gridreader.read()) { } gridreader.close(); dt= new datatable("budget"); da.fill(dt); datagrid_budget.itemssource = dt.defaultview; conn.close(); } private void datagrid_budget_roweditending(object sender, system.windows.controls.datagridroweditendingeventargs e) { datagridrow editedrow = e.row; int row_index = (datagrid)sender).itemcontainergenerator.indexfromcontainer(editedrow); (int k=0;k< 5;k++) { datagridcell cell = getcell(row_index, k); textblock tb = cell.content textblock; if (k==1) { dt.rows[row_index][k] = tb.text; } else if (k == 4) { if (tb.text != "") { dt.rows[row_index][k] = convert.todatetime(tb.text); } } else { dt.rows[row_index][k] = convert.toint32(tb.text); } } da.updatecommand = new sqlcommandbuilder(da).getupdatecommand(); da.update(dt); } public datagridcell getcell(int row, int column) { datagridrow rowcontainer = getrow(row); if (rowcontainer != null) { datagridcellspresenter presenter = getvisualchild<datagridcellspresenter>(rowcontainer); datagridcell cell = (datagridcell)presenter.itemcontainergenerator.containerfromindex(column); if (cell == null) { datagrid_budget.scrollintoview(rowcontainer, datagrid_budget.columns[column]); cell = (datagridcell)presenter.itemcontainergenerator.containerfromindex(column); } return cell; } return null; } public datagridrow getrow(int index) { datagridrow row = (datagridrow)datagrid_budget.itemcontainergenerator.containerfromindex(index); if (row == null) { datagrid_budget.updatelayout(); datagrid_budget.scrollintoview(datagrid_budget.items[index]); row = (datagridrow)datagrid_budget.itemcontainergenerator.containerfromindex(index); } return row; } public static t getvisualchild<t>(visual parent) t : visual { t child = default(t); int numvisuals = visualtreehelper.getchildrencount(parent); (int = 0; < numvisuals; i++) { visual v = (visual)visualtreehelper.getchild(parent, i); child = v t; if (child == null) { child = getvisualchild<t>(v); } if (child != null) { break; } } return child; }
Comments
Post a Comment