winforms datagridview calculated field change event

前端 未结 3 537
没有蜡笔的小新
没有蜡笔的小新 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条回答
  •  萌比男神i
    2021-01-13 17:36

    It seems that your problem comes from the fact that your calculated column is part of the DataTable and not part of the DataGridView. I'm not sure if the DataGridView exposes an event for whenever the dgv's underlying data source changes. There is DataGridView.DataSourceChanged event, but that only seems to fire when you actually set the DataSource property, not when the DataTable changes itself with your calculations.

    If you want to make it work without changing the structure of your code, subscribe to the DataTable's RowChanged event and you can scan the dgv and update your label from there. It's not exactly a clean solution, but it works. I would consider switching to a calculated column in the DataGridView if that's an option.

提交回复
热议问题