如何根据另一行datagridview总计一行?

时间:2016-01-25 04:26:14

标签: c# winforms datagridview

我是新来的,但我想问一些事情。我有

enter image description here

我希望在dataGridTwo中添加一行时自动填充dataGridOne。在dataGridTwo数量单元格中,dataGridOne数量单元格与相同的dataGridOne运输容器单元格相加。

enter image description here

2 个答案:

答案 0 :(得分:1)

您希望处理您的"装运箱中的单元格的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)
    }
}

答案 1 :(得分:0)

 private void cellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
    {
        //Only handles cases where the cell contains a TextBox
        var editedTextbox = e.EditingElement as TextBox;

        if (editedTextbox != null)
          {                
             //your logic of quntity
             grid2.rows.add();
             grid2.rows[grdi.rowcount-1].cell["Cell Name"].value=qty;
          }

    }