c# - Changing Quantity value within Datagridview,the price also changes.how possilbe? which event will do this.CellEndEdit already used -
i want change quantity value within datagridview,it willl change price also.how possilbe? event using c# windows form.cellendedit used other cell(productname).
private void datagridviewsalesform_cellendedit(object sender, datagridviewcelleventargs e) { try { string matchingproduct = datagridviewsalesform.rows[0].cells[1].value.tostring(); (int = 0; < objarraylist.count; i++) { if (objarraylist[i].tostring().tolower().startswith(matchingproduct.tolower())) { datagridviewsalesform.rows[0].cells[1].value = objarraylist[i].tostring(); break; } } } catch (exception ex) { messagebox.show(ex.message); } /*connection loading brandname brandtable*/ string get_strproductname = datagridviewsalesform.rows[0].cells[1].value.tostring(); string quantity = "1"; sqlconnection conn = new sqlconnection(connstr); try { conn.open(); string qry = "select t_inv_brands.brand_name t_inv_products inner join t_inv_brands on t_inv_products.brand_id = t_inv_brands.brand_id t_inv_products.prod_name ='" + get_strproductname + "'"; sqlcommand cmd = new sqlcommand(qry, conn); sqldataadapter da = new sqldataadapter(cmd); dataset ds = new dataset(); da.fill(ds); datagridviewsalesform.rows[0].cells[2].value = ds.tables[0].rows[0]["brand_name"].tostring(); datagridviewsalesform.rows[0].cells[3].value = quantity; conn.close(); } catch (exception ex) { messagebox.show(ex.message); } /*connection loading retailprice producttable*/ sqlconnection connection = new sqlconnection(connstr); try { conn.open(); string qry = "select retailprice t_inv_products prod_name = '" + get_strproductname + "'"; sqlcommand cmd = new sqlcommand(qry, connection); sqldataadapter da = new sqldataadapter(cmd); datatable dt = new datatable(); da.fill(dt); datagridviewsalesform.rows[0].cells[4].value = convert.todouble(dt.rows[0]["retailprice"].tostring()); } catch (exception ex) { messagebox.show(ex.message); } //double quantityload = convert.todouble(datagridviewsalesform.rows[0].cells[3].value.tostring()); //double pricefetch = convert.todouble(datagridviewsalesform.rows[0].cells[4].value.tostring()); //double result = quantityload * pricefetch; //datagridviewsalesform.rows[0].cells[4].value = result.tostring(); }
you seem think can use event one cell or column only. instead need code events in such way all necessary cells and/or columns own piece of logic.
it helps partition code smaller pieces helpful names document logic contain!
here example two branches, 1 cell , 1 column. pass out datagridviewcelleventargs
functions can access datagridview dgv
real event can.. of course can expand on needed:
int quantitycolumnindex = 3; // use own numbers.. int currencycolumnindex = 1; // ..and names!! int currencyrowindex = 0; int priceperunitcolumnindex = 7; int totalpricecolumnindex = 8; int totalbasepricecolumnindex = 4; private void dgv_cellendedit(object sender, datagridviewcelleventargs e) { if (e.columnindex == quantitycolumnindex) dopricecalc(e); else if (e.columnindex == currencycolumnindex && e.rowindex == currencyrowindex) doallcalc(e); } void dopricecalc(datagridviewcelleventargs e) { // 1st example dgv[totalpricecolumnindex, e.rowindex].value = (int)dgv[quantitycolumnindex, e.rowindex].value * (decimal)dgv[priceperunitcolumnindex, e.rowindex].value; } void doallcalc(datagridviewcelleventargs e) { // 2nd example decimal currency = (decimal) dgv[currencycolumnindex,currencyrowindex ].value; (int row = 0; row < dgv.rows.count; row++) dgv[priceperunitcolumnindex, e.rowindex].value = (decimal)dgv[totalbasepricecolumnindex, e.rowindex].value * currency; }
note have indexed columns indeices. may index them names, e.g.: dgv[ "priceperunit", e.rowindex]
Comments
Post a Comment