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

use a for loop for that, u ll get the total in a local variable...

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

SELECT SUM([PRICE]) AS TOTAL FROM MYTABLE

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

addie20,
I couldn't get your question, what should happen with your grid dynamically..?

@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..

commented: Good examples, shame about the missing [CODE] tags :) +1

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;

@Darpan,
It was the same thing that I had posted before...
Nice try, u made it short...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.