Event Handling for ColumnChanging

C

coconet

I have a .NET 2.0 DataTable with a ColumnChanging event. There is code
in the event that tests ProposedValue and if it is "a" then the value
I want in the column should be "1". The column is Type Int32.

Main program (C#) flow is

string teststring = "a";
DataRow datarow = mytable.NewRow();
mytable.Rows.Add(datarow);
datarow["IntColumn"] = teststring;

Event code snippet is

if ( e.Column.ColumnName == "IntColumn" )
{
if ( e.ProposedValue.ToString() == "a" )
{
e.Row[e.Column] = 1;
}
}


The data is first properly set to 1, then the ColumnChaning event
fires again but the "if" conditional fails so control goes back to the
main program/method. Then the ' datarow["IntColumn"] = teststring;'
line runs and an Exception is thrown because a string is going into
the column again.

I would like to keep my logic code in the ColumnChanging event to
convert the input string to an int where I need it. What is the best
way to make that happen? And how can I make the event only fire once?




Thanks.
 
W

WenYuan Wang [MSFT]

Hello Coconet,

According to your description, your main concern is that ColumnChanging
event fires twice in your application, correct? If I misunderstood
anything, please don't hesitate to correct me.

When the ProposedValue is "a", you modify the current filed to 1. This
result the ColumnChanging event fired again, because you modified current
row.

My suggestion is that: We needn't to modify the underling datarow directly.
In ColumnChanging event, proposedValue will be assigned to current field.
Modifying proposedValue is the correct method to change the current value.
In addition, it will not accuse ColumnChanging event fires again. This is
because we never touch the underlying data.
For example:
if ( e.Column.ColumnName == "IntColumn" )
{
if ( e.ProposedValue.ToString() == "a" )
{
e.ProposedValue = 1;
}
}

Hope this helps. Please feel free to update here again, if there is
anything unclear. We are glad to assist you.

Have a great day,
Best regards,
Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

WenYuan Wang [MSFT]

Hello Coconet.

This is Wen Yuan, again. Have you tried my method?
Does it help? I just want to check if there is anything we can help with.
Please feel free to update here, if you face any furthe issue.

Have a great day,
Best regards,

Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

WenYuan Wang [MSFT]

Hello coconet,

Thanks for your reply. You are welcome.

Have a great day,
Best regards,

Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

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