winforms datagridview calculated field change event

前端 未结 3 563
没有蜡笔的小新
没有蜡笔的小新 2021-01-13 17:15

I have a datagridview bound from a datatable. I have a calculated column linetotal that multiples unitprice * quantity.

I also have a totalprice label,that I would

3条回答
  •  梦谈多话
    2021-01-13 17:36

    Normally I test out my answers before I give them, but I'm operating away from my dev PC, so I'm not 100% sure which route is best, but here are some ideas to try out.

    On a DataGridView, you can use the CellEndEdit event to tell when a cell has been edited. there's also a CellLeave event, but I haven't used it and yo may need to experiment with it.

    On a DataGrid, you can use the CurrentCellChanged event.

    However, to do any calculations, you will need to extract your values fron the underlying Dataable or DataView (depending on what the control is bound to). I'v also found that using the DataGrid.CurrentCellChanged event is buggy and it's throwwn when the DataGrid is databaound, not just when a user edits the contents, so you may need to contend with that... I would imagine that the DataGridView's CellLeave event might have similar issues. Like I said, I'm not 100% sure, so you'll need to experiment.

    Also, as Kyle suggested, you could try to use events from the underlying DataSource.

    DataTable.RowChanged or DataView.ListChanged events wouldbe where I would start.

    As I said, I am unable to test these myself, but hopefully one of these ideas will point you in the right direction.

提交回复
热议问题