Click here to Skip to main content
15,033,303 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi guys, I want to multiply or add the values of two or more cells in a datagridview control in a row and and should also be auto-saved in the database after entering datas in the cell

I don't know what code to use, so I did not put any codes in this discussion.
My Image Link
http://tinypic.com/view.php?pic=142bryf&s=8
Thanks In Advance
Posted

Hi Fellow...

Used following guideline for performing Multiple or Add operations on DGV's individual cells to generate Auto-Calculated values for any other Cell in DGV


If you Really want to do programming by Heart...You can transform following steps into your Code Sytle...

Put following code into DGV's CellValueChanged Event.
C#
if (e.RowIndex > -1)
{ 
     if (DGV.Rows[e.RowIndex].Cells[1].FormattedValue.ToString() != "")
        {
          //look for which column is being Changed and 
         //After checking which Cell's value is Changed, 
         //Calculate the "Total Amount" value for Auto-Calculated Cell as follows...
          if (e.ColumnIndex == 1 || e.ColumnIndex == 2)
          DGV.Rows[e.RowIndex].Cells[3].Value = 
                (decimal)(Cells[1].value)  * (decimal)(Cells[2].value);
         }     
}


//Now to Calculate Sum of Each Column's Values...
//you can use following code snippet
XML
<pre lang="cs">decimal total = 0;
    for (int i = 0; i &lt; rows; i++)
       total += decimal)(DGV.Rows[i].Cells[3].Value);
           </pre>


Note :- This is just Guideline and I know it's working I used it in my one of project. Here I just give a major points that you are looking for...

And The Code for Saving these DATA into database you should have to Try it by yourself.. I think you are very familiar with saving / retrieving data from Database.


Happy Programming...:-)
   
There is nothing like Auto-Save. You have to handle some event of DataGridView or any other control's event like a Button Click or something.

You can try to handle the DataGridView.CellLeave Event[^]. Inside that retrieve the cell value and do the needed operations. After that connect to database and insert that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900