this is in c# vs2003, web forms
i have this dgv and which is populated by some database values i have one column called price and i want to get the total of that column to a local veriable can some one tell me how to do it

If the data is in a database then do a seperate aggregate query.
E.G.

SELECT SUM([PRICE]) AS TOTAL FROM MYTABLE

Edited 6 Years Ago by nick.crane: n/a

can some 1 help me with the code for Dynamic grid view in a Windows form not a web form using C#

@kalpa23,
are u changing the values in the grid..?
or, if you are just populating the data in a grid (i.e if it is not editable, just add a column in ur SP that you are calling, SUM([Amount]) from Table.

I could sum the values of datagrid view rows. Following is the code


              decimal sumdr = 0;

             decimal sumcr = 0;

            decimal sumtot = 0;

 

            foreach (DataGridViewRow  grdRows in dgvBankBibaran.Rows)

            {

 

                sumdr = sumdr + Convert.ToDecimal(grdRows.Cells[10].Value);

                sumcr = sumcr + Convert.ToDecimal(grdRows.Cells[11].Value);

 

 

            }

            sumtot = sumdr - sumcr;

            txtTot.Text =  sumtot.ToString();

try this way

dtDisp_changed = objBusiness.Display_Inv_dtl(invoiceNoTextEdit.Text.Trim());

for (int i = 0; i < dtDisp_changed.Rows.Count; i++)
{
if(i == 0)
sum = Convert.ToDecimal(dtDisp_changed.Rows[0]["AMT"]);

if(i>0)
sum = sum + Convert.ToDecimal(dtDisp_changed.Rows["AMT"]);
}

MessageBox.Show(Convert.ToString(sum));

where dtDisp_changed is the DataTable,
["AMT"] is the column that you want the addition to be done....

Please mark the thread as solved once this works.

Thanks..

am sry, i forgot to add a condition between., find the proper code below...

dtDisp_changed = objBusiness.Display_Inv_dtl(invoiceNoTextEdit.Text.Trim());

if(dtDisp_changed.Rows.Count > 0)
{
for (int i = 0; i < dtDisp_changed.Rows.Count; i++)
{
if(i == 0)
sum = Convert.ToDecimal(dtDisp_changed.Rows[0]["AMT"]);

if(i>0)
sum = sum + Convert.ToDecimal(dtDisp_changed.Rows["AMT"]);
}
}

MessageBox.Show(Convert.ToString(sum));

where,
dtDisp_changed is the DataTable,
["AMT"] is the column that you want the addition to be done....

Please mark the thread as solved once this works.

Thanks..

Comments
Good examples, shame about the missing [CODE] tags :)

decimal price=0;
decimal tot=0;
int rowCount=this.dgv.Rows.Count;

for (int i=0; i<rowCount;i++)
{
price=price+Convert.toDecimal(this.dgv.Rows.Cells["Price"].value);
}
tot=price;

This article has been dead for over six months. Start a new discussion instead.