GridView 的 RowsAdded 事件不起作用
我有一个 GridView,其中包含 代码、经验、薪水、奖金 列
数据 代码、经验 和 薪水 列来自 BindingSource
,我需要根据经验和薪水计算奖金数据。
因此,我为 DataGridView
创建了 RowsAdded
事件并添加了以下代码
int index = employeeBindingSource.Position;
Employee emp = (Employee)employeeBindingSource.Current;
double bonus = (emp.Experience < 4 ? 0.08f : 0.12f) * emp.Salary * 12;
employeeDataGridView.Rows[index].Cells[3].Value = bonus.ToString("0.00"); //setting value for bonus column
employeeBindingSource.MoveNext();
,但我只获得了两行的奖励。
因此我评论了该事件中的代码并创建了另一个事件 RowStateChanged
添加了上面的代码,它工作正常。
RowsAdded
事件有什么问题? RowStateChanged
是计算奖金列的正确事件吗?
谢谢。
I have a GridView which contains columns Code, Experience, Salary, Bonus
Data for Code, Experience and Salary columns come from BindingSource
and I need to calculate data for Bonus based on Experience and Salary.
So I have created RowsAdded
event for the DataGridView
and added the following code
int index = employeeBindingSource.Position;
Employee emp = (Employee)employeeBindingSource.Current;
double bonus = (emp.Experience < 4 ? 0.08f : 0.12f) * emp.Salary * 12;
employeeDataGridView.Rows[index].Cells[3].Value = bonus.ToString("0.00"); //setting value for bonus column
employeeBindingSource.MoveNext();
But I get Bouns for only two rows.
So I commented the code inside this event and created another event RowStateChanged
added the above code and it works fine.
What is the problem with the RowsAdded
event ? And is RowStateChanged
the correct event to calculate the bonus column ?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你正朝着错误的方向前进。
从设计的角度来看,向数据源添加额外的字段会更容易、更正确吗?
如果您的数据源是类型化对象的列表,则只需使用 getter 添加属性即可。
如果您的数据源是
DataSet/DataTable
,您可以添加计算列。I think you are heading in wrong direction.
Would it be easier, yet more correct from design point of view to add extra fields to you data source.
If you data source is a list of typed objects, you could just add a property only with getter.
If you data source is
DataSet/DataTable
you can add a calculated column.