views:

158

answers:

2

Okay big brains here's something that's more of a challenge than a requirement. I am a bit stumped. I usually just need a prod in the right direction, so get your prodding sticks ready.

I have a tabcontrol covered in textboxes. I want to perform a check of the contents of all the textboxes during the SelectedIndexChanged event on a listview on the same form. If one of the textboxes has data different from a DataTable row - represented by the ListView Item - I want it to ask if the user would like to keep the change they just made. If nothing has changed I want it to just change the selection.

So obviously I'm comparing the contents of the text boxes against associated columns in the datarow.

I could just brute force the check and do each individual check one at a time. I'd prefer to come up with some clever algorithmic way of cycling through the tabcontrol textboxes and checking the values against the columnar values.

Any suggestions?

EDIT: I like the "cleverly named textboxes" solution below best, although both are good. If no one else has a better idea in the next 14 days the textbox answer gets the green.

A: 

Give the textboxes a clever name as in a portion of the name is the column/row name.

Group the textbox controls an loop through them. For each control, get the (portion)name and use that as a reference to your datatable. Check the values.

PoweRoy
A: 

If I'm understanding you right, you want to avoid comparing every textbox on every change, in favour of just checking the textboxes that are changed, driven by the SelectedIndexChanged event of the ListView control. Is that right?

Well, DataRows and DataTables already have row versioning and rollbacks implemented, so if you bind the text boxes to the underlying row (either by writing events to write back on change/lose focus or by using an automated mechanism to accomplish the same task), then check the RowState property on SelectedIndexChanged. If the RowState is anything other than unchanged, prompt the user to save. If he saves, commit the changes, otherwise reject them.

So, for example, you'd want something like this in your SelectedIndexChanged event handler:

if (row.RowState == DataRowState.Modified) {
  // prompt for user input
  if (promptResult == PromptResult.Save) {
    row.AcceptChanges();
  }
  else {
    row.RejectChanges();
  }
}
Welbog