C# .NET - How to calculate cells in Datagridview

Asked By Deven on 21-Jan-11 01:44 AM
Hello,
I want to Calculate the values of cells in display it in another column
This is my coding:

 SqlDataAdapter da = new SqlDataAdapter("select Date,BT_CD as 'Code',DL_Name,Trade,Script_Name,Qty,Rate,Brokerage,Net_Amount from Buy where Client_Name='" +
                     comboBox2.Text + "' " + " Union " +
                     "select Date,ST_CD as 'Code' ,DL_Name,Trade,Script_Name,Qty,Rate,Brokerage,Net_Amount from Sell where Client_Name='" + comboBox2.Text + "' ORDER by Date asc", con);
                DataSet ds = new DataSet();
                da.Fill(ds);
                dataGridView1.DataSource = ds.Tables[0];
if (dataGridView1.ColumnCount == 9)
                        {
                            dataGridView1.Columns.Add(column);
                            dataGridView1.Columns["Date"].DisplayIndex = 0;
                            dataGridView1.Columns["Code"].DisplayIndex = 1;
                            dataGridView1.Columns["DL_Name"].DisplayIndex = 2;
                            dataGridView1.Columns["Trade"].DisplayIndex = 3;
                            dataGridView1.Columns["Script_Name"].DisplayIndex = 4;
                            dataGridView1.Columns["Qty"].DisplayIndex = 5;
                            dataGridView1.Columns["Rate"].DisplayIndex = 6;
                            dataGridView1.Columns["Brokerage"].DisplayIndex = 7;
                            dataGridView1.Columns["Net_Amount"].DisplayIndex = 8;
                        }

I want to add the value of "Net_Amount"  where "Code"=BT..... & subtract the value of "Net_Amount" where "Code"=ST.....and Display it in another column.
Kindly help me if you can.

Regards,
Deven.
Rohan Dave replied to Deven on 21-Jan-11 03:07 AM
hey can you please explain clearly ? what you mean by "I want to add the value of "Net_Amount"  where "Code"=BT" ? But the question is what you want to add in Net_Amount ?
Deven replied to Rohan Dave on 21-Jan-11 07:26 AM
Hi,
There are Datagridview columns retrieved from the database like this :
 Code          Qty         Rate      Net_Amount      Total
 BT0001       100         200         20000.00        +20000.00
 ST0001       100         250         25000.00          - 5000.00
 BT0002       100         100         10000.00          +5000.00
 ST0002       100         150         15000.00         -10000.00

I had added new column named "Total" and want to calculate the Net_Amount value as (add where Code=BT.....and subtract where Code=ST......)
Hope you'll get it.

Regards,
Deven. 
Rohan Dave replied to Deven on 21-Jan-11 10:58 PM
hey Deven - sorry to say but still your explaination is not clear to me... do you want to show " + " sign before the actual amount in " Total " column ? Which columns value you want to add/substract in " Total " column ? you already have " Net_Amount " column in your gridview then what the use of " Total " column ?
Deven replied to Deven on 22-Jan-11 12:21 AM
Hi,
The Total column is the new column added in gridview & I want to display the calculation of Net_Amount column(which is retrieved from database) in the Total column.
The + sign shows credit balance & - sign shows debit balance.
The Net_Amount is added inthe Total column where Code=BT...... & substracted where Code=ST......
In short it is the ledger of the Client.
Code          Qty         Rate      Net_Amount      Total
 BT0001       100         200         20000.00        +20000.00
 ST0001       100         250         25000.00          - 5000.00
 BT0002       100         100         10000.00          +5000.00
 ST0002       100         150         15000.00         -10000.00

Kindly help me calculating the column.
Regards,
Deven.