如何根据DatagGridView中绑定列的值计算未绑定列值?(How to calculate unbound column value based on value of bound colum

编程入门 行业动态 更新时间:2024-10-22 20:27:57
如何根据DatagGridView中绑定列的值计算未绑定列值?(How to calculate unbound column value based on value of bound colum in DatagGridView?)

我的DataGridView中有几列,其中一列是未绑定列,DataGridVIew位于VirtualMode中。

当调用CellValueNeeded事件时,我想基于Cells[2]的值计算Cells[0]的值,该值位于有限列中,位于底层DataSource 。

这就是我尝试这样做的方式:

private void dgvItems_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e) { e.Value = dgvItems.CurrentRow.Cells[2].Value * 5; //simplified example }

但是,我收到System.StackOverflowException因为它dgvItems.CurrentRow.Cells[2].Value调用dgvItems.CurrentRow.Cells[2].Value导致调用另一个CellValueNeeded事件。 等等......然而,Cells [2]不是一个未绑定的列,所以在常识上它不应该导致递归调用,除非获取任何列(绑定或未绑定)的值来激活该事件...

我不能在这里使用SQL Expression,我不能在任何SQL调用中预先计算e.Value。 在实例中,Cell [2] .Value是HashTable中使用的一个键,它将返回Cells [0](e.Value)的正确值。

我能做什么?

I have few columns in my DataGridView, one of them is an unbound column and the DataGridVIew is in VirtualMode.

When CellValueNeeded event is called, I want to calculate value of Cells[0] basing on the value of Cells[2] which is in bounded column to the underlaying DataSource.

This is how I try to do this:

private void dgvItems_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e) { e.Value = dgvItems.CurrentRow.Cells[2].Value * 5; //simplified example }

However, I am getting System.StackOverflowException because it seams that call to dgvItems.CurrentRow.Cells[2].Value results in call to another CellValueNeeded event. And so on and so on... However Cells[2] is not an unbound column, so on common sense it should not result in recursive call unless getting value of any column(bound or unbound) firest that event...

I can not use here SQL Expression and I can not precalculate e.Value in any SQL call. In real example Cells[2].Value is a key used in HashTable which will return a correct value for the Cells[0] (e.Value).

What can I do?

最满意答案

您需要有选择性并仅对第0列采取行动:

//untested private void dgvItems_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e) { if (e.Column == 0) { e.Value = dgvItems.CurrentRow.Cells[2].Value * 5; //simplified example } }

You need to be selective and act on column 0 only:

//untested private void dgvItems_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e) { if (e.Column == 0) { e.Value = dgvItems.CurrentRow.Cells[2].Value * 5; //simplified example } }

更多推荐

本文发布于:2023-08-05 17:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1435772.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   unbound   calculate   DatagGridView   bound

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!