的WinForms DataGridView的计算领域的变化事件 [英] winforms datagridview calculated field change event

查看:109
本文介绍了的WinForms DataGridView的计算领域的变化事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一个DataTable绑定一个DataGridView。我有一个计算列linetotal的倍数单价*数量。

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

我也有一个totalprice的标签,我想包含在DataGridView的linetotal列的总和。

I also have a totalprice label,that I would like to contain the sum of the linetotal column in the datagridview.

当用户作出任何更改或单价数量,linetotal应该更新该行和totalprice标签应该总结的linetotal列。

When the user makes any changes to unitprice or quantity, the linetotal should update for that line and the totalprice label should sum the linetotal column.

我似乎无法找到使用计算totalprice标签正确的事件。我试过cellvaluechanged,currentcelldirtystatechanged,rowleave。他们都不似乎工作的权利。我想我需要所有的linetotal列后,大火被计算的事件

I can't seem to find the correct event to use to calculate the totalprice label. I've tried cellvaluechanged, currentcelldirtystatechanged, rowleave. None of them seems to work right. I guess I need an event that fires after all the linetotal columns are calculated.

我的伪代码:

dt = getallitems(itemnumber);
dt.Columns.Add("LineTotal", typeof(double));
dt.Columns["Linetotal"].Expression = "[unitprice] * [quantity]";

dgv.DataSource = dt;

private void dgv_WhatEventToUse???(object sender, DataGridViewCellEventArgs e)
{
    //method to iterate the rows of the datagridview, and sum linetotal column
}

必须有一些事件,当所计算的列的值被改变的探测器。

There must be some event that fires when the value of the calculated column is changed.

推荐答案

看来你的问题来自于一个事实,你的计算列是数据表的一部分,而不是在DataGridView的一部分。我不知道,如果在DataGridView暴露了每当DGV的底层数据源的事件。有DataGridView.DataSourceChanged事件,但似乎只当你真正设定DataSource属性,而不是当数据表本身的变化与你的计算。

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.

如果你想火使其在不改变代码的结构工作,订阅DataTable的RowChanged事件,你可以扫描DGV,并从那里更新您的标签。这不完全是一个干净的解决方案,但它工作。我会考虑切换到如果这是一个选项,在DataGridView计算列。

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.

这篇关于的WinForms DataGridView的计算领域的变化事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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