在行更改之前提交DataGridView中的更改 [英] Commiting changes in DataGridView before Row Change

查看:206
本文介绍了在行更改之前提交DataGridView中的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用绑定到数据库的DataGridView。我们试图将其中一列计算为数据库中某些其他列的平均值。

We're working with a DataGridView bound to a database. We are trying to have one of the columns calculated as an average of some of the other columns from the database.



    • 我们无法在数据库中创建计算列,也无法在数据集中创建一个列。如果有办法这样做,我们的问题就解决了。



< AvgSkill = Avg(Skill1,Skill2,Skill3)

Ex: AvgSkill = Avg(Skill1, Skill2, Skill3)


  1. 由于我们似乎无法创建一个计算列,我们尝试创建一个单元格事件处理程序将计算新的平均值,然后使用新信息更新数据库,但是,一旦我们更改单元格,并尝试更新数据库,我们将丢失所有添加到该行的数据。

我们已经弄清楚,当您离开行时,dataGridView只会更新绑定的数据源,所以当我们尝试进行更新时,它会擦除​​所有输入的内容在这行中。

We've figured out that the dataGridView only updates the bound data source when you leave the row, so when we try to do an update, it erases anything entered in that row.

有没有办法强制datagridview在我们离开行之前更新我们的事件处理程序中的绑定数据源?更新tableadapter似乎没有这样做。

Is there a way to force the datagridview to update the bound data source in our event handler before we leave the row? Updating the tableadapter doesn't seem to do it.

提前感谢任何帮助:)

thanks in advance for any help :)

- Dominique

-Dominique

推荐答案

我想你想在单元格更改事件处理程序中执行以下操作:

I think you want to do the following in the cell change event handler:

    private void dgvPlayers_CellValidated(object sender, DataGridViewCellEventArgs e)
    {
         this.Validate();
    }

强制每个单元格更改DataGridView的验证。您可以通过检查e.ColumnIndex将此操作限制为特定的列。您还可以包含同时进行计算所需的任何代码。

Forces the validation of the DataGridView with every cell change. You could limit this action to specific columns by checking e.ColumnIndex. You could also include any code required to make calculations at the same time.

;)

这篇关于在行更改之前提交DataGridView中的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆