0

I have 400+ textboxes on a page. The page is meant to be an exact replica of a paper form. The user is going to edit all of the fields and then save the data to the DB. The data is being pulled down into a DataTable from a SQL DB.

I'm planning on saving the data via the same DataTable or just via a bulk update. Not 100% on that. But, I need to get access to that data. And maybe I'm not doing this next best part the best and if I'm not, I'd appreciate it if I was informed of a better way.

When the DataTable gets the data, I assign each field into the appropriate control. Below is an example of how the data is being added.

foreach (DataRow CDR in ClaimDataTable.Rows){
    if (CDR["BoxNumber"].ToString() == "1.1")
        this.Box1_1.Text = CDR["DataValue"].ToString();
    if (CDR["BoxNumber"].ToString() == "1.2")
        this.Box1_2.Text = CDR["DataValue"].ToString();
    if (CDR["BoxNumber"].ToString() == "1.3")
        this.Box1_3.Text = CDR["DataValue"].ToString();

I wrote some code to automatically create that code. So I didn't manually write all 400+ lines.

What I was thinking, was that I could add a LostFocus event to each TextBox. Then when the control loses focus, I would create a class with a box name and the box value. Add that to a list and when they're ready to save, just loop through the list and do the bulk update with the BoxNumber and the box data.

Would that be feasible? Or is there a better method?

Thanks!

ernest
  • 1,633
  • 2
  • 30
  • 48

2 Answers2

3
  1. Look into event routing.
  2. You should not create and access individual text-boxes but let the framework create them via datatemplating a collection, your above code is ze horror.
H.B.
  • 166,899
  • 29
  • 327
  • 400
  • I'm not sure I understand you. I have to create individual textboxes because the page has to look exactly like the paper form. – ernest Jun 25 '13 at 15:12
  • @ernest you're probably doing it all wrong. Please post a screenshot of what you need and we can tell you the proper way to do it in WPF. Also, please don't use `System.Data`. That sucks. Create a proper data model instead. By the way. If you have tons of data you'd better create a ViewModel instead of manipulating all UI elements ala-winforms. – Federico Berasategui Jun 25 '13 at 15:21
  • @HighCore http://i.imgur.com/cWYAKsL.gif. That's an example of what the form looks like. I have to replicate that form in XAML. Which is about 80% done. There's 400+ editable fields that the user has to be able to edit and save. – ernest Jun 25 '13 at 15:24
  • @ernest: Depends on your layout, using the `ItemsControl.ItemsPanel` you can control how your items are laid out. If the boxes are all over the place and not systematic like a grid or the like you may need to create them separately. Nonetheless, you can use event routing for the purposes of your primary question. – H.B. Jun 25 '13 at 15:24
  • @ernest: Just saw your screenshot, some of the TextBoxes belong together, at least for those groups you could use individual `ItemsControls` to lay them out. – H.B. Jun 25 '13 at 15:26
  • 100% agree with @H.B. ItemsControl is the way to go here, not to mention you should be using databinding, and not the horrible code-behind type of hacks from crapforms (aka winforms) – Federico Berasategui Jun 25 '13 at 15:27
  • @ernest All I see in your screenshot is a lot of `ItemsControls`. Maybe a `DataGrid` too. – Federico Berasategui Jun 25 '13 at 15:30
  • 1
    @ernest: Also replicating a paper form in software is not a good idea to put it nicely. I would only offer the option to output it like that for printing separately. – H.B. Jun 25 '13 at 15:30
  • @H.B. I'll look into ItemControls. Making the paper form in XAML isn't up to me. It's what the client wants. – ernest Jun 25 '13 at 15:38
  • @ernest you should also look into creating a proper data model (EDMX or otherwise) and using MVVM. – Federico Berasategui Jun 25 '13 at 15:40
  • @HighCore I wanted to use MVVM, but I don't have the time now to go back to it. It's probably something I can do at a later point, but not at the moment. – ernest Jun 25 '13 at 15:59
  • @ernest not really, all your code is completely useless and you will have to delete it all and start all over once you understand what's the correct way to program in WPF. – Federico Berasategui Jun 25 '13 at 16:02
  • @HighCore I know how to program in WPF and I know MVVM. It's just not something I can right now. Most of the code won't have to be re-written. It'll just have to be added to VMs. This part is a tiny fraction of what's needed. – ernest Jun 25 '13 at 16:05
  • @ernest yeah, sure. And your ViewModels will be as crappy and bloated because you're just moving the code behind to that, instead of using `ItemsControls`. – Federico Berasategui Jun 25 '13 at 16:07
  • 1
    @ernest: Of course it's what the client wants, but the client often enough has no idea about *what they need*. – H.B. Jun 25 '13 at 16:11
2

When the DataTable gets the data, I assign each field into the appropriate control.

WPF is data binding and one does not generally load controls as you mentioned. Why is the code not binding to each control to a property which adheres to INotifyPropertyChanged?

  1. Have the page's DataContext (DC) point to an instantiated class which adheres to InotifyPropertyChanged interface. The setting of the DC in that fashion will allow all controls on the page to use its datacontext by default.
  2. In the class to be instantiated for the DC create a member property which calls PropertyChanged when the value is set using the INotifyPropertyChanged system.
  3. Bind each TextBox Text property to that property on the class.

Then each control which needs that value will display it automatically after it has been set such as in this example.

<TextBlock Name="tbHeader"
           Text="{Binding CDRData}" />

Then one only has to write the value to the property named CDRData once and all textboxes bound get the value.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
  • Thanks, OmegaMan. It's going to be a paint, but I'm going to change the project to MVVM and then and go with your suggestion. – ernest Jun 25 '13 at 16:21
  • @ernest The VM is the class I mentioned for the page. You have already started with MVVM. :-) Note have the constructor of the VM call the database and load the bound properties. That way when you set the page's DC to an newly instantiated VM it starts the process of getting data. – ΩmegaMan Jun 25 '13 at 16:24