C# 如何基于另一行datagridview合计一行?
我是新来的,但我想问一些问题。我有 我想在C# 如何基于另一行datagridview合计一行?,c#,winforms,datagridview,C#,Winforms,Datagridview,我是新来的,但我想问一些问题。我有 我想在dataGridOne中添加一行时自动填充dataGridTwo。在dataGridTwoQuantity单元格中,将dataGridOne数量单元格与相同的dataGridOneShipping Container单元格相加 您需要为“装运容器”列中的单元格处理CellEndEdit事件: 其思想是,每当“Shipping Container”列中的单元格发生更改时,您将遍历所有行,计算每个容器的出现次数。然后用这些计数更新dataGridTwo;根
dataGridOne
中添加一行时自动填充dataGridTwo
。在dataGridTwo
Quantity单元格中,将dataGridOne
数量单元格与相同的dataGridOne
Shipping Container单元格相加
您需要为“装运容器”列中的单元格处理
CellEndEdit
事件:
其思想是,每当“Shipping Container”列中的单元格发生更改时,您将遍历所有行,计算每个容器的出现次数。然后用这些计数更新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)
}
}
private void DataGridOne\u CellEndEdit(对象发送方,DataGridViewCellEventArgs e)
{
//ColumnName=装运集装箱列的名称
if(dataGridOne.Columns[e.ColumnIndex]==dataGridOne.Columns[“ColumnName”])
{
字典总和=新字典();
//对于不是dataGridOne中新行的每一行
//key=行的单元格[“ColumnName”]中的值(即“巨型行李”)
//如果sums包含键,则递增sums值
//否则,值为1
//对于不是dataGridTwo中新行的每一行
//key=行的单元格[“ColumnName”]中的值(即“巨型行李”)
//如果sums包含键,则将单元格值设置为sums值
//否则将单元格值设置为0
//从内存中删除密钥
//对于每个剩余的KeyValuePair(总和)
//使用(键、CRIS ID、值)向dataGridTwo添加新行
}
}
网格是在相同的表单上还是在不同的表单上?网格是在相同的表单上。如果我在dataGridOne
中添加一行,例如。|…|1234 | Mega Bag Glass | 250
,应如何更新dataGridTwo
?1) Mega-Bag-Glass |…|3
;或2)Mega-Bag-Glass |……|900
。您的屏幕截图暗示了选项1,但您的措辞暗示了选项2。@Oh如果您像这样向dataGridOne
添加一行,dataGridTwo
将像您的第一个选项1一样更新,Mega Bag Glass |……|3
。对不起,我的英语不好。对不起,我的手机没有文本框。默认情况下,它会将手机视为文本框
dataGridOne.CellEndEdit += DataGridOne_CellEndEdit;
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)
}
}