views:

64

answers:

1

I have a lightweight data-basey kind of thing. Functionally, I'd like to read and write tables out to disk(plus maybe some other info in the future).

I chose to use the DataSet object, paired with the DataGridView control for the IO. The DataSet object can serialize out to disk and back again via the handle XML read/write functions.

However, there does not seem to be an obvious way to add information to the DataView control and have the DataSet object updated. Among other things, the DataGridView won't add a row for me in run-time and won't let me programmatically run the addrow method.

Clearly, I'm missing something.

What am I missing?


This has been suggested:

dataSet1.Tables["mytable"].Rows.Add();

But System.Data.DataRow does not permit direct construction, and I want to add a blank row for the DataGridView to fill in?

edit2: I'm dumb. There's a new row function.

Hmm. I'm wondering if this is just a Wrong Approach.

+1  A: 

Don't modify the content of the DataGridView programmatically, modify the DataSet itself instead. It will automatically update the DataGridView if it is databound to the DataSet (using the DataSource and DataMember properties). Changes made by the user in the DataGridView will be automatically reflected in the DataSet.


UPDATE

dataSet1.Tables["mytable"].Rows.Add();

But System.Data.DataRow does not permit direct construction, and I want to add a blank row for the DataGridView to fill in?

You can construct a DataRow manually :

DataRow row = dataSet1.Tables["mytable"].NewRow();
row["column1"] = value1;
row["column2"] = value2;
...
dataSet1.Tables["mytable"].Rows.Add(row);

If you just want a blank row, just don't initialize the fields...

If you have a BindingSource between the DataSet and the DataGridView, call AddNew on the BindingSource. It will add a blank line. You can also use a BindingNavigator control, which handles the calls to the BindingSource methods.

Thomas Levesque
Umph. I'll putz with this later. It's not writing in/out correctly. In another note, Architecture Astronaut detected in MS system design. >.<
Paul Nathan