views:

113

answers:

1

I have a form that will be populated by a bunch of information from a template, and then handed over to the user to fill out the rest. The thing is, the template and the object that holds the eventual result are DataRows, because it's all going into and out of a database (that I have no schema control over), so this seems like a great place to just bind all the controls and get on with more important things, right?

I can't get the controls to bind properly, and this is partly my fault because I wanted to just throw it a row, have it work out which columns from the row are important, and keep the values from those, so I'm frequently throwing it rows that don't have all the columns the controls expect, and so they throw an exception. But it's partly I-don't-know-who's fault because doing this:

inProductName.DataBindings.Add("Text", TemplateBinder, "ProductName");

where TemplateBinder is a BindingSource object with the DataSource property set to the DataRow I want to trickle out, throws an exception when I go to show the form. Not really sure what I should do differently - the TemplateBinder won't contain any DataSource until I get the template row, and if I have to manage the bindings myself instead of setting them and forgetting them at the top of the page I'm not getting much value out of binding.

And this is just a test to see if what I'm trying to do is even feasible! I'm trying to pass the BindingSource a row that contains the values I want to propagate to the form, suspend binding, switch it out for a blank row from the table so the values I propagated out stay in the properties I've bound, show the form, let the user fill it out and then turn on binding again so the bound properties in the controls trickle into the blank row I've set as the BindingSource DataSource. I mean, there's no way that'll work, because that'd be too easy, but it'd sure be nice if I could do that instead of my old way which involved a 30-case switch statement.

I don't want to go too far down the rabbit hole here. There's a tempting button in Visual Studio that says it'll just set everything all up automatically, but I have experience with Visual Studio that suggests it won't like the idea that the schema of the database won't be changed through Visual Studio. In addition, I have no idea how to get the flexibility I (or rather, my clients) demand from this fancy automagic setup (I can't even see how to programmatically mess with the connection string). The copy-pasted code I have is probably less of a sin than killing features because I made a sweeping change behind the scenes that happens to make certain very handy features inconvenient. Copypasta can be turned into a function a lot quicker than it takes to rewrite the entire application. And then what if the database changes? Automagic code seems to prefer to throw an exception instead of just swallowing its pride and not doing anything if it doesn't have enough information to do something.

So, Stack Overflow, the tl;dr: I want to make data binding work for me instead of making me throw out everything I've done. Can I?

+1  A: 

I expect that for that to work, you'd need to use a DataRowView, not a DataRow; try getting this from the DataView rather than the DataTable. The default view is table.DefaultView.

For info; the reason it works when you use a DataTable as the binding-source is that DataTable implements IListSource, and returns (from GetList()) the default view - so actually, when you bind to a DataTable, you actaully end up binding to the default view (DataView).

Marc Gravell