views:

58

answers:

1

Hi.

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?

A: 

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
  }
}
Henk Holterman