DataGridView CellValueChanged触发RowsRemoved

时间:2017-08-04 14:11:25

标签: c# datagridview

我有一个C sharp应用程序,它会在单元格中的值发生更改时重新计算成本。到目前为止,我的DataGridView有一个CellValueChanged()事件,在更改单元格值时会触发该事件。但是它也会触发DataGrid的RowsRemoved()事件 我已使用RowIndex事件中RowCount类型对象的DataGridViewRowsRemovedEventArgsRowsRemoved()记录了这些事件,即使单元格值已更改,它也表明RowsRemoved()中受影响的行就是所有行。

        Debug.WriteLine("Row removed: " + e.RowIndex);
        Debug.WriteLine("Row removed count: " + e.RowCount);



CellValueChanged()事件下的代码:

if (dgridDetails.Rows.Count > 0){ 
            SetNewQuantity(e.RowIndex, Int32.Parse(dgridDetails.Rows[e.RowIndex].Cells["Quantity"].Value.ToString()));
            RefreshDataGridView();
            Debug.WriteLine("Cell changed row: " + e.RowIndex);
            Debug.WriteLine("Cell changed column: " + e.ColumnIndex);
            Debug.WriteLine("New quantity: " + dgridDetails.Rows[e.RowIndex].Cells["Quantity"].Value);

        }

RefreshDataGridView()方法:

decimal newTotalCost = 0;
        dgridDetails.Rows.Clear();
        dgridDetails.Refresh();
        foreach (var myLineItem in allItems)
        {
            //re add items on datagrid
            dgridDetails.Rows.Add(myLineItem.GetItemNumber(), myLineItem.GetItemName(),
                myLineItem.GetItemPrice(), myLineItem.GetQuantity(), myLineItem.GetCost());

            //keep new total figure
            newTotalCost += myLineItem.GetCost();
        }
        //show new total cost
        lblTotalCost.Text = newTotalCost.ToString();


如何更改单元格值时,如何才能触发RowsRemoved()?

0 个答案:

没有答案