如何在datagridview的最后一行显示datagridview列总数 [英] How to show total of datagridview column total in datagridview last row

查看:434
本文介绍了如何在datagridview的最后一行显示datagridview列总数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我在数据网格视图中有2列,现在我想在数据网格视图的最后一行显示总数如何做到这一点请帮助我并给我最好的建议与示例



我尝试过:



int sum = 0;

for(int i = 0; i< dataGridView1.Rows.Count; ++ i)

{

sum + = Convert.ToInt32(dataGridView1.Rows [i ] .Cells [1] .Value);

}

textBox1.Text = sum.ToString();

int sum1 = 0;

for(int i = 0; i< dataGridView1.Rows.Count; ++ i)

{

sum1 + =转换。 ToInt32(dataGridView1.Rows [i] .Cells [2] .Value);

}

textBox3.Text = sum1.ToString();

I want to know I have 2 columns in data grid view and now i want to show total of columns in data grid view last row how can do this please help me and give me best suggestion with example

What I have tried:

int sum = 0;
for (int i = 0; i < dataGridView1.Rows.Count; ++i)
{
sum += Convert.ToInt32(dataGridView1.Rows[i].Cells[1].Value);
}
textBox1.Text = sum.ToString();
int sum1 = 0;
for (int i = 0; i < dataGridView1.Rows.Count; ++i)
{
sum1 += Convert.ToInt32(dataGridView1.Rows[i].Cells[2].Value);
}
textBox3.Text = sum1.ToString();

推荐答案

在计算之后你需要添加另一行,除非它已经存在(如果希望每当你尝试添加时它都被归零)。



You will need to add another row after doing the calculations, unless its is already there (which if it is hopefully it is zeroed whenever you try to do this adding).

dataGridView1.Rows.Add();





然后你只需要设置最后一行中每个单元格的值。





Then you just need to set the value of each cell in the last row.

int rowIdx = dataGridView1.Rows.Count - 1;
dataGridView1.Rows[rowIdx].Cells[1].Value = sum;
dataGridView1.Rows[rowIdx].Cells[2].Value = sum1;


这篇关于如何在datagridview的最后一行显示datagridview列总数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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