我是新来的,想请教一下。我有
我想在 dataGridOne
中添加一行时自动填充我的 dataGridTwo
。在 dataGridTwo
Quantity 单元格中,dataGridOne
Quantity 单元格与相同的 dataGridOne
Shipping Container 单元格相加。
最佳答案
您需要为“运输容器”列中的单元格处理 CellEndEdit
事件:
dataGridOne.CellEndEdit += DataGridOne_CellEndEdit;
想法是每当“运输容器”列中的单元格发生更改时,您将遍历所有行,计算每个容器的出现次数。然后您将使用这些计数更新 dataGridTwo
;根据需要添加任何缺少的容器。
不要带走所有的乐趣,代码/算法:
private void DataGridOne_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
// ColumnName = the Name of the Shipping Container Column
if (dataGridOne.Columns[e.ColumnIndex] == dataGridOne.Columns["ColumnName"])
{
Dictionary<string, int> sums = new Dictionary<string, int>();
// For each row that's not the new row in dataGridOne
// key = the value in cell["ColumnName"] of the row (i.e. "Mega Bags")
// if sums contains the key, increment sums value
// else sums value is 1
// For each row that's not the new row in dataGridTwo
// key = the value in cell["ColumnName"] of the row (i.e. "Mega Bags")
// if sums contains the key, set the cell value to sums value
// else set the cell value to 0
// remove the key from sums
// For each remaining KeyValuePair in sums
// add a new row to dataGridTwo using (key, CRIS ID, value)
}
}
关于c# - 如何根据另一行数据 GridView 汇总一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34985082/