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?
Cor Ligthert [MVP] - 29 Jan 2006 08:48 GMT
David,
It is not a nice solution, however we have the method for this on our
website (which is a VB Net website so no C# however it should be clear the
difference is almost less than nothing)
http://www.vb-tips.com/default.aspx?ID=30d9d2fd-9f10-4928-b7c8-1def3152436f
I hope this helps,
Cor
Otis Mukinfus - 29 Jan 2006 19:32 GMT
>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
[quoted text clipped - 11 lines]
>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
Cor Ligthert [MVP] - 29 Jan 2006 20:36 GMT
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
>>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
[quoted text clipped - 36 lines]
> http://www.otismukinfus.com
> http://www.tomchilders.com
Otis Mukinfus - 29 Jan 2006 22:10 GMT
>David, in addition to Otis,
>
[quoted text clipped - 8 lines]
>
>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.G. Ryan - MVP - 30 Jan 2006 14:37 GMT
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();
}
}
>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
[quoted text clipped - 11 lines]
> need the calculated column to display the result before the user moves
> to a new row. Is this possible?
David Kirkman - 30 Jan 2006 22:23 GMT
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