【发布时间】:2014-11-17 17:42:55
【问题描述】:
我有一个带有两个数据网格视图的 C# 应用程序,我希望第一个数据网格计算位置并将其放在第二个数据网格视图的两个位置。
例如:这是我的第一个数据网格InputDataGrid:
Position | Description | Value
-------------------------------
001 | test | 2,5
002 | | 1
001 | hello | 1,5
002 | test2 | 2
如果我单击一个按钮,我希望此数据在此表单的第二个数据网格中 -> ResultDataGrid
Position | Value
----------------
001 | 4
002 | 3
这是我的代码:更新
private void btnCalc_Click(object sender, EventArgs e) {
foreach (DataGridViewRow row in InputDataGrid.Rows)
{
if (row.Cells[0].Value == null)
{
break;
}
string position = (string)row.Cells[0].Value;
Double sellvalue = Convert.ToDouble(row.Cells[2].Value);
foreach (DataGridViewRow resultrow in ResultDatagrid.Rows)
{
if ((string)resultrow.Cells[0].Value == position)
{
Double oldvalue = Convert.ToDouble(resultrow.Cells[1].Value);
Double newValue = oldvalue + sellvalue;
resultrow.Cells[0].Value = Convert.ToString(newValue);
}
else
{
resultrow.Cells[0].Value = position;
resultrow.Cells[1].Value = Convert.ToString(sellvalue);
ResultDatagrid.Rows.Add(resultrow);
}
}
}
}
【问题讨论】:
-
从你的问题中看不清楚你想要达到什么或者你有什么问题,请解释一下。
-
我希望我的应用程序计算相同的位置并将其显示在第二个 datagridview 中
-
和什么位置一样?为什么您无法做到这一点,您需要帮助解决什么问题?
标签: c# winforms visual-studio-2010 datagrid cells