954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

data grid view help ..........................

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

kalpa23
Light Poster
41 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
 

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

arunkumars
Junior Poster
186 posts since Jul 2009
Reputation Points: 26
Solved Threads: 22
 

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

SELECT SUM([PRICE]) AS TOTAL FROM MYTABLE
nick.crane
Nearly a Posting Virtuoso
1,230 posts since Feb 2010
Reputation Points: 375
Solved Threads: 187
 

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

addie20
Newbie Poster
4 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

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

arunkumars
Junior Poster
186 posts since Jul 2009
Reputation Points: 26
Solved Threads: 22
 

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

arunkumars
Junior Poster
186 posts since Jul 2009
Reputation Points: 26
Solved Threads: 22
 
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

hirenpatel53
Posting Whiz in Training
221 posts since Jun 2010
Reputation Points: 10
Solved Threads: 27
 

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[i]["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..

arunkumars
Junior Poster
186 posts since Jul 2009
Reputation Points: 26
Solved Threads: 22
 

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[i]["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..

arunkumars
Junior Poster
186 posts since Jul 2009
Reputation Points: 26
Solved Threads: 22
 

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

for (int i=0; i

Darpan Singh
Newbie Poster
1 post since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

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

arunkumars
Junior Poster
186 posts since Jul 2009
Reputation Points: 26
Solved Threads: 22
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: