如何将datagrdview单元格的总和添加到同一个datagridview [英] How to get sum of datagrdview cells to the same datagridview

查看:73
本文介绍了如何将datagrdview单元格的总和添加到同一个datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如下例所示:



id价格价格总价



1 25 10 30 ...



2 5 10 10 ...



3 10 20 10 ...



我想在DataGrid中将这三列相加。



id价格总价格



1 25 10 30 65



2 5 10 10 25



3 10 20 10 40





这三个价格来自三张桌子......我点击一下按钮总计



我尝试过的事情:



i尝试了这个,但它只给了冷杉t行总数



int cell1 = Convert.ToInt32(dataGridView1.CurrentRow.Cells [2] .Value);

int cell2 = Convert .ToInt32(dataGridView1.CurrentRow.Cells [3] .Value);

int cell3 = Convert.ToInt32(dataGridView1.CurrentRow.Cells [4] .Value);

int cell4 = Convert.ToInt32(dataGridView1.CurrentRow.Cells [5] .Value);

int cell5 = Convert.ToInt32(dataGridView1.CurrentRow.Cells [6] .Value);

int cell6 = Convert.ToInt32(dataGridView1.CurrentRow.Cells [7] .Value);

int cell7 = Convert.ToInt32(dataGridView1.CurrentRow.Cells [8] .Value);



if(cell1.ToString()!=&& cell2.ToString()!=&& cell3.ToString()!=&& cell4.ToString()!=&& cell5.ToString()!=&& cell6.ToString()!=&& cell7.ToString()!=)

{



dataGridView1.CurrentRow.Cells [0] .Value = cell1 + cell2 + cell3 + cell4 + cell5 + cell6 + cell7;



}

Like the following example:

id price price price total

1 25 10 30 ...

2 5 10 10 ...

3 10 20 10 ...

I want to sum of three columns like this in the DataGrid.

id price price price total

1 25 10 30 65

2 5 10 10 25

3 10 20 10 40


these three price from three tables ...i get the total by one button click

What I have tried:

i have tried this but it gives only first row total

int cell1 = Convert.ToInt32(dataGridView1.CurrentRow.Cells[2].Value);
int cell2 = Convert.ToInt32(dataGridView1.CurrentRow.Cells[3].Value);
int cell3 = Convert.ToInt32(dataGridView1.CurrentRow.Cells[4].Value);
int cell4 = Convert.ToInt32(dataGridView1.CurrentRow.Cells[5].Value);
int cell5 = Convert.ToInt32(dataGridView1.CurrentRow.Cells[6].Value);
int cell6 = Convert.ToInt32(dataGridView1.CurrentRow.Cells[7].Value);
int cell7 = Convert.ToInt32(dataGridView1.CurrentRow.Cells[8].Value);

if (cell1.ToString() != "" && cell2.ToString() != "" && cell3.ToString() != "" && cell4.ToString() != "" && cell5.ToString() != "" && cell6.ToString() != "" && cell7.ToString() != "")
{

dataGridView1.CurrentRow.Cells[0].Value = cell1 + cell2 + cell3 + cell4 + cell5 + cell6 + cell7;

}

推荐答案

我假设代码显示在按钮单击处理程序中。如果是这样,你的问题是CurrentRow只会成为当前活动的SINGLE行。



虽然我不认为这是一个好方法,以下将接近你的意图(在问题中):



I'm assuming the code shown is in the button click handler. If so, your problem is that "CurrentRow" is only ever going to be the SINGLE row that is currently active.

While I do not think this is a good approach, the following would come close to your intent (in the question):

foreach (DataGridViewRow row in dataGridView1.Rows)
{
  int total = 0;
  for (int index = 1; index < row.Cells.Count; index++)
    total += Convert.ToInt32(row.Cells[index].Value);
  row.Cells[0].Value = total;
}





除此之外,我不确定如何建议你。我需要更多信息。



你选择的用户界面有点奇怪。你可能会考虑不同的东西,但是你可能会有一个奇怪的要求:)



我可能还建议改变对象列表或数据表,支持网格查看,而不是直接更改网格视图本身。然后,你可以重新触发数据绑定。



但是,我再次从你的问题中缺乏足够的信息,以确定这是最好的建议。



Beyond that, I'm not sure how to advise you. I'd need more information.

The user-interface you've chosen is a bit odd. You might consider something different, but then again perhaps you have an odd requirement :)

I would probably also suggest altering the list of objects or data table, backing the grid view, instead of directly altering the grid view itself. Then, you can re-trigger data binding.

However, here again I lack adequate information from your question to be certain this is the best advice.


这篇关于如何将datagrdview单元格的总和添加到同一个datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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