DataGridView:如何获得grandtotal-?在窗体上 [英] DataGridView : how to get grandtotal-? on windows form

查看:97
本文介绍了DataGridView:如何获得grandtotal-?在窗体上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在datagridview中总计col1 ..



thx提前



col1



10

30

-----

60

解决方案

尝试:

 受保护  void  GridView1_RowDataBound( object  sender,GridViewRowEventArgs e)
{
double amount = 0 ;
// 检查特定行的RowType
if (e.Row.RowType == DataControlRowType.DataRow)
{
amount + = Convert.ToDouble(e.Row.Cells [ 1 ] Text.Trim())。
GridView1.FooterRow.Cells [ 1 ]。文本= amount.ToString( #,### 00\" );
}
}


您可以获取DataGridView的对象,并在foreach循环中循环遍历每一行并继续添加它们到总金额。



 DataGridView dgv = dataGridView1; 
int amount = 0 ; // 如果您需要,可以使用双倍

< span class =code-keyword> foreach (DataGridViewRow row in dgv)
{
amount + = Convert.ToInt32( row.Amount);
}

// 循环结束后


在gridview中添加数据源

在分配后添加一行

 Dgv.AllowUserToAddRows =  false ; 
Dgv.Rows。 add ();





<数据网格视图中的
单元格值更改

 如果 (e.columnindex ==  1  //  列索引您希望求和的列 
{
double amount = 0 ;
For(i = 0 ; i< = dgv.rows.count-2; i ++)
{
amount + = Convert .ToDouble(dgv.Row [i] .Cells [ 1 ]。Text.Trim());
}
dgv.rows [i] .cells [ 1 ]。 value = amount.ToString();
}





请注意将相同的代码放入行 - 删除事件中......



快乐编码!

:)


i need grand total of col1 in datagridview..

thx in advance

col1
20
10
30
-----
60

解决方案

Try:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
        double amount = 0;
        //Check the RowType of the specific row
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            amount += Convert.ToDouble(e.Row.Cells[1].Text.Trim());
            GridView1.FooterRow.Cells[1].Text = amount.ToString("#,###.00");
        }
 }


You can take an object of DataGridView and in a foreach loop you can loop through each row and keep adding them to the total amount.

DataGridView dgv = dataGridView1;
int amount = 0; //maybe you can use double if that is what you need

foreach (DataGridViewRow row in dgv)
{
    amount += Convert.ToInt32(row.Amount);
}

//After the loop ends 


while you add datasource in gridview
put a line after you assign it

Dgv.AllowUserToAddRows = false;
Dgv.Rows.add();




in datagridview cell value change

if (e.columnindex == 1)//column index of column you want to sum
{
    double amount = 0;
    For (i=0; i<= dgv.rows.count-2;i++)
    {
      amount += Convert.ToDouble(dgv.Row[i].Cells[1].Text.Trim());
    }
    dgv.rows[i].cells[1].value = amount.ToString();
}



Note put same code in row - deleted event also...

Happy Coding!
:)


这篇关于DataGridView:如何获得grandtotal-?在窗体上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆