Datagrid calculated column not updating

D

David Kirkman

I have a datagrid (C# Windows Forms App) bound to a dataset. One column
in the dataset is a calculated column based on an expression that
simply subtracts the value in one column from the value in another
column. I created the dataset using a dataset control and the
properties dialog boxes. The dataset is not populated from a database -
the data is entered into the grid by the user.
By adding the following line of code in the form load event handler:

dtChequeNos.Columns["line_count"].Expression = "to_seq_no -
from_seq_no";

I was able to get the column to perform the calculation but it only
displays the result when the user moves to a new row in the grid. I
need the calculated column to display the result before the user moves
to a new row. Is this possible?
 
O

Otis Mukinfus

I have a datagrid (C# Windows Forms App) bound to a dataset. One column
in the dataset is a calculated column based on an expression that
simply subtracts the value in one column from the value in another
column. I created the dataset using a dataset control and the
properties dialog boxes. The dataset is not populated from a database -
the data is entered into the grid by the user.
By adding the following line of code in the form load event handler:

dtChequeNos.Columns["line_count"].Expression = "to_seq_no -
from_seq_no";

I was able to get the column to perform the calculation but it only
displays the result when the user moves to a new row in the grid. I
need the calculated column to display the result before the user moves
to a new row. Is this possible?
Yes, but you will need to make the column that displays the calculated
value a "nonCalculated column, otherwise (I think) the calculation
will be done twice if you use the example:

Use the ColumnChanged event to calculate the value. Here is an
example of the ColumnChanged event from the VS 2005 help files (VS
2003 will be the same).

You may have to refresh the row to make the new value display in the
column you calculated.

private static void Column_Changed(object sender,
DataColumnChangeEventArgs e )
{
Console.WriteLine("Column_Changed Event: name={0}; Column={1};
original name={2}",
e.Row["name"], e.Column.ColumnName, e.Row["name",
DataRowVersion.Original]);
}

HTH

Otis Mukinfus
http://www.otismukinfus.com
http://www.tomchilders.com
 
C

Cor Ligthert [MVP]

David, in addition to Otis,

Be aware that AFAIK it seems that the column changed event is working in all
versions different.

(In my idea does in the version 2003 nothing when it is in the same row in
the datagrid, because the datagrid is telling nothing then to the
underlaying datasource, that hapens at a datarowchange)

However I can be wrong, it is just AFAIK (as far as I know).

Cor

Otis Mukinfus said:
I have a datagrid (C# Windows Forms App) bound to a dataset. One column
in the dataset is a calculated column based on an expression that
simply subtracts the value in one column from the value in another
column. I created the dataset using a dataset control and the
properties dialog boxes. The dataset is not populated from a database -
the data is entered into the grid by the user.
By adding the following line of code in the form load event handler:

dtChequeNos.Columns["line_count"].Expression = "to_seq_no -
from_seq_no";

I was able to get the column to perform the calculation but it only
displays the result when the user moves to a new row in the grid. I
need the calculated column to display the result before the user moves
to a new row. Is this possible?
Yes, but you will need to make the column that displays the calculated
value a "nonCalculated column, otherwise (I think) the calculation
will be done twice if you use the example:

Use the ColumnChanged event to calculate the value. Here is an
example of the ColumnChanged event from the VS 2005 help files (VS
2003 will be the same).

You may have to refresh the row to make the new value display in the
column you calculated.

private static void Column_Changed(object sender,
DataColumnChangeEventArgs e )
{
Console.WriteLine("Column_Changed Event: name={0}; Column={1};
original name={2}",
e.Row["name"], e.Column.ColumnName, e.Row["name",
DataRowVersion.Original]);
}

HTH

Otis Mukinfus
http://www.otismukinfus.com
http://www.tomchilders.com
 
O

Otis Mukinfus

David, in addition to Otis,

Be aware that AFAIK it seems that the column changed event is working in all
versions different.

(In my idea does in the version 2003 nothing when it is in the same row in
the datagrid, because the datagrid is telling nothing then to the
underlaying datasource, that hapens at a datarowchange)

However I can be wrong, it is just AFAIK (as far as I know).

Cor
[snip]

Thanks, Cor. I was not aware the two versions had different behavior.

Otis Mukinfus
http://www.otismukinfus.com
http://www.tomchilders.com
 
W

W.G. Ryan - MVP

David:

The problem is that the Expression isn't calclated until after the edit is
complete. So when you're making the change in the row in the grid, it's
still in Edit mode. by calling EndEdit on the row, or EndCurrentEdit on the
bindingManager/context that the grid's bound to, the refresh will take
place. To facilitate this, you can check for the ColumnChanging or the
CurrentCellChanging. Depending on how many expressions you have in the
table, you may want to trap different events. In this example, I trap
CurrentCellChange and just end the edit, which causes the Expression to
recompute. You could add functionality to check for your specific column or
depending on the column, take a different course of action. The main thing
to understand though is why it's not updating and that's b/c the row is
still in Edit mode (which for example, as far as its concerned, means you
could undo the changes you just made) so it won't change until you've
finished the edit. Here's code that will do it for you:

private void dataGridView1_CurrentCellChanged(object sender, EventArgs e)

{

if (dataGridView1.CurrentRow != null && dataGridView1.CurrentRow.Index >= 0)

{

dt.Rows[dataGridView1.CurrentRow.Index].EndEdit();

}

}
 
D

David Kirkman

Thanks for the help guys.
I forgot to mention that I am using 2003. In the end I removed the
expression from the column and set the value in the CurrentCellChanged
event handler:

protected void dataGrid1_CurrentCellChanged(object sender, EventArgs e)
{
int rowNum = dataGrid1.CurrentCell.RowNumber;
int colNum = dataGrid1.CurrentCell.ColumnNumber;
if (colNum == 2 || colNum == 5)
{
dataGrid1[rowNum, 6] = (int)dataGrid1[rowNum, 4] -
(int)dataGrid1[rowNum, 1];
}
}

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top