c# - Rows cannot be programmatically added to the datagridview rows collection when the control is data-bound -


i'm new winform c#.so have problem when trying add row datagridview

public void sourcefordatagridview(string str)     {         try         {             sqldataadapter da = new sqldataadapter(str, cnn);             datatable dt = new datatable();             da.fill(dt);             bindingsource bsource = new bindingsource();             bsource.datasource = dt;             datagridview1.datasource = bsource;             da.update(dt);         }         catch (exception ex)         {             messagebox.show(ex.message);         }     } 

that's source datagridview1,

sourcefordatagridview("select tenthuoc,dvtinh,sl,dongia,thanhtien,hsd ctnhapthuoc join thuoc on ctnhapthuoc.mathuoc = thuoc.mathuoc join nhapthuoc on nhapthuoc.manhapthuoc = ctnhapthuoc.manhapthuoc ctnhapthuoc.manhapthuoc = '" + txtmapnt.text.trim() + "' "); 

i want try add newrow when click button value datagridview2

            double sum = 0;             sum = double.parse(txtdgmua.text.trim().tostring()) * double.parse(txtslmua.text.trim().tostring());             bool existed = false;             (int = 0; < datagridview1.rows.count; i++)             {                 int selectedrowindex = datagridview2.selectedcells[0].rowindex;                 datagridviewrow selectedrow = datagridview2.rows[selectedrowindex];                 string = convert.tostring(selectedrow.cells[0].value);                 string b = convert.tostring(datagridview1.rows[i].cells[0].value);                 if (a == b)                 {                     existed = true;                     datagridview1.rows[i].cells[2].value = int.parse(datagridview1.rows[i].cells[2].value.tostring()) + int.parse(txtslmua.text);                     //sum += (double.parse(txtdgmua.text.trim().tostring())*double.parse(txtslmua.text.trim().tostring()));                     datagridview1.rows[i].cells[4].value = double.parse(datagridview1.rows[i].cells[2].value.tostring()) * double.parse(datagridview1.rows[i].cells[3].value.tostring());                     break;                 }             }             if (!existed)             {                 try                 {                     //object[] item = { this.datagridview2.currentrow.cells[0].value.tostring(), this.datagridview2.currentrow.cells[1].value.tostring(), txtslmua.text, txtdgmua.text, sum, dtphsd.value };                     this.datagridview1.rows.add(this.datagridview2.currentrow.cells[0].value.tostring(), this.datagridview2.currentrow.cells[1].value.tostring(), txtslmua.text, txtdgmua.text, sum, dtphsd.value);                 }                 catch (exception ex)                 {                     messagebox.show(ex.message);                 }             } 

but , if(!exists) , this's not working. please me.

instead of inserting datagridviewrow try insert datarow datasource

var datasource = datagridview1.datasource bindingsource; var datatable = datasource.datasource datatable;  datatable.rows.add(value1, value2,...) 

this update bindingsource , show new values of datatable.


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -