Editing a control that is not databound

Lesson learned this week.  We have a form with controls bound to a DataTable.  When editing a field in the DataTable that was not bound to a control, we tried to do the following:

1) Get the current DataRow

2) with the current DataRow, set the DataColumns new value: DataRow.Item("my_datacolumn") = "new value"

This never worked. 

So we then added a control on the form and bound it to "my_datacolumn" .  We hid this control under another control so the user would not see it.  We turned off it's TabStop so a user could not tab into it. 

At run time, when we wanted  to edit the field we would set focus onto the control, set it's text property then set focus to another control.  Why did we do this?  To force the validated event to fire on the control to databinding would push the new value to the datacolumn.  

This works though it's a hack. 

A better solution?

Use a DataRowView instead of the current DataRow.

When a DataRow is in the middle of an edit  transaction and you want to update a field at runtime, and the field is not bound, update the DataView, not the DataRow.

Thanks to Mark Boulter at MS for this tip.

 

No Comments