Edit in datagridview successful, but it does not update the certain column


can edit data in database. having problem, when edit , update quantitycolumn in datagridview, supposed update the total column in datagridview based onquantity * subtotal , update the quantity column, not price.

did wrong?

here screenshot:

enter image description here

can see in rows "1", the quantity is 100 , the subtotal is 10000, the total will 1000000. in rows "2", the quantity before edit in datagridview, quantity is 100and the total is 2000000, when change the quantity to 500, the total still 2000000, the total supposed 10000000.

did wrong?

here code (i post code necessary needed):

private void updated(object sender, eventargs e)          {              datatable _dt = (datatable)datagridview1.datasource;                if (_dt.defaultview.count > 0)              {                  int rownum = datagridview1.currentrow.index;                  string productcode = convert.tostring(_dt.defaultview[rownum]["productcode"]);                  int quantity = convert.toint32(_dt.defaultview[rownum]["quantity"]);                  int price = convert.toint32(_dt.defaultview[rownum]["subtotal"]);                  int _price = convert.toint32(_dt.defaultview[rownum]["total"]);                    using (oledbconnection conn = new oledbconnection(connectionstring))                  {                      string commandselect = "select [quantity], [subtotal], [total] [record] [productcode] = @productcode";                      string commandupdate = "update [record] set [quantity] = @quantity, [subtotal] = @subtotal, [total] = @total [productcode] = @productcode";                      string _commandselect = "select [quantity], [subtotal], [total] [transrecord] [productcode] = @productcode";                      string _commandupdate = "update [transrecord] set [quantity] = @quantity, [subtotal] = @subtotal, [total] = @total [productcode] = @productcode";                        conn.open();                        using (oledbcommand cmdselect = new oledbcommand(_commandselect, conn))                      using (oledbcommand cmdupdate = new oledbcommand(_commandupdate, conn))                      using (oledbcommand _cmdselect = new oledbcommand(commandselect, conn))                      using (oledbcommand _cmdupdate = new oledbcommand(commandupdate, conn))                      {                          cmdselect.parameters.add("@productcode", system.data.oledb.oledbtype.varchar);                          cmdselect.parameters["@productcode"].value = productcode;                            _cmdselect.parameters.add("@productcode", system.data.oledb.oledbtype.varchar);                          _cmdselect.parameters["@productcode"].value = productcode;                            cmdupdate.parameters.add("@quantity", system.data.oledb.oledbtype.integer);                            _cmdupdate.parameters.add("@quantity", system.data.oledb.oledbtype.integer);                            cmdupdate.parameters.add("@subtotal", system.data.oledb.oledbtype.integer);                            _cmdupdate.parameters.add("@subtotal", system.data.oledb.oledbtype.integer);                            cmdupdate.parameters.add("@total", system.data.oledb.oledbtype.integer);                            _cmdupdate.parameters.add("@total", system.data.oledb.oledbtype.integer);                            using (oledbdatareader dreader = _cmdselect.executereader())                          {                              while (dreader.read())                              {                                  cmdupdate.parameters.add("@productcode", system.data.oledb.oledbtype.varchar);                                  _cmdupdate.parameters.add("@productcode", system.data.oledb.oledbtype.varchar);                                    cmdupdate.parameters["@productcode"].value = productcode;                                  cmdupdate.parameters["@quantity"].value = quantity;                                  cmdupdate.parameters["@subtotal"].value = price;                                  cmdupdate.parameters["@total"].value = _price;                                    _cmdupdate.parameters["@productcode"].value = productcode;                                  _cmdupdate.parameters["@quantity"].value = quantity;                                  _cmdupdate.parameters["@subtotal"].value = price;                                  _cmdupdate.parameters["@total"].value = _price;                                    int numberofrows = _cmdupdate.executenonquery();                                  int _numberofrows = cmdupdate.executenonquery();                              }                                dreader.close();                          }                      }                        conn.close();                  }          }    private void updateprice(object sender, eventargs e)          {              if (numerictextbox1.textlength >= 6)              {                  decimal quantity = convert.toint32(this.numericupdown1.value);                  decimal price = convert.todecimal(this.numerictextbox2.text);                  int total = convert.toint32(quantity * price);                    if (numericupdown1.value > 0)                  {                      this.numerictextbox3.text = total.tostring();                  }              }          }

the total is working when not in datagridview, in here (shown screenshot):

enter image description here

when goes datagridview , when change the quantity, not update the totalbased on subtotal * quantity

appreciate , answer!

thank much!

hi fuhanspujisaputra,

whenever edit in datagird , changes some value it limited datagrid only. not reflected in dataset/datatable. before going update of database need loop through datagrid , find out changed 1 , re calculation.

in following code snippet saing not working, probable reason may be..

 for (int = 0; < datagridview1.rows.count; i++)
           
{
                quantity
= convert.toint32(this.datagridview1.rows[i].cells[2].value);
                price
= convert.toint32(this.datagridview1.rows[i].cells[4].value);
           
}

 you not adding values, must in following way..

for (int = 0; < datagridview1.rows.count; i++)
           
{
                quantity +
= convert.toint32(this.datagridview1.rows[i].cells[2].value);
                price +
= convert.toint32(this.datagridview1.rows[i].cells[4].value);
           
}
and think supposed use cells 1 , 3 quantity , price.



Visual Studio Languages  ,  .NET Framework  >  Visual C#



Comments

Popular posts from this blog

Azure DocumentDB Owner resource does not exist

job syspolicy_purge_history job fail in sqlserver 2008

Trying to register with public marketplace error with 'Get-AzureStackStampInformation'