Click to See Complete Forum and Search --> : How do I update a DataGridView before saving?


RobR
February 15th, 2011, 10:29 AM
I'm playing with data binding for the first time. When I started, I thought it would be easy. But I've run into so many gotchas, my remaining supply of hair is rapidly diminishing.

I have a simple table with four columns, one of which is a primary key that has no relation to reality. I tried adding the other three columns to my DataSet, but when I dragged the thing (set? adapter? whatever it is that appeared for that table in my data source window) onto my form, the automatically generated adapter didn't have an Update() method.

So, I added the primary key field to the data source thing. Now my data grid has a column I don't care about. I set that column's Visible property to False. When I ran my program, the column was visible anyway.

So, I set the column's ReadOnly property to True. That one took. Now, when I add a record, that column gets a nonsense value, but in the database, the key is updated according to the sequence it's attached to, instead of using the nonsense value. I can live with that.

There's one last thing that is bugging me, and I can't figure it out. The three non-key columns are all marked Not Null. If I add a row, enter a value for each of the first two columns, and then get to third column, type in a number, and then click my Save button, I get an error because the value of the third column is null, even though I just typed a number for it. If I enter a number for the third column and then click on a different cell and then click my Save button, it works.

The code behind my Save button is:

GaugeGapSource.EndEdit();
GaugeGapAdapter.Update(this.modelTables.gauge_gap);

If I was writing in C++, I could just call the DataGridView's UpdateData() method, but this is C#, and I don't know what the corresponding call is. How can I get this to work the way I think it should?

Thanks very much!

RobR