GridView 的 RowsAdded 事件不起作用

发布于 2024-11-15 12:10:58 字数 980 浏览 3 评论 0原文

我有一个 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.

enter image description here

So I commented the code inside this event and created another event RowStateChanged added the above code and it works fine.

enter image description here

What is the problem with the RowsAdded event ? And is RowStateChanged the correct event to calculate the bonus column ?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风挽心 2024-11-22 12:10:58

我认为你正朝着错误的方向前进。

从设计的角度来看,向数据源添加额外的字段会更容易、更正确吗?
如果您的数据源是类型化对象的列表,则只需使用 getter 添加属性即可。

 public decimal Bonus
 {
     get { return (Experience < 4 ? 0.08f : 0.12f) * Salary * 12; }
 }

如果您的数据源是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.

 public decimal Bonus
 {
     get { return (Experience < 4 ? 0.08f : 0.12f) * Salary * 12; }
 }

If you data source is DataSet/DataTable you can add a calculated column.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文