views:

2038

answers:

1

I have a DataGridView, which loads data from mdb Access table
The grid only shows data (is readonly). I have a button for inserting new row, and now have to make two more buttons, one for update and one for delete

The following code works fine for inserting a new row

this.estacionamientoTableAdapter.Insert(tb1.Text, tb2.Text, tb3.Text, null, null);
this.dataGridView1.EndEdit();
this.estacionamientoTableAdapter.Fill(estacionarDataSet.Estacionamiento);
this.dataGridView1.Refresh();

Can anyone provide me a sample for editing data from selected row, and for deleting a selected row from the grid? Of course using tableAdapter, Dataset, etc.?

A: 

you need to populate the UpdateCommand and EditCommand for your table.

something along the lines of:

this.estacionamientoTableAdapter.Adapter.UpdateCommand = new System.Data.SqlClient.SqlCommand("update statement",this.connection);

Alternatively if you are useing autogenerated code that was generated from a table without a primary key and you have the possibility to do so - then add a primary key and re-generate your table adapter.

dice
Yes, I use autogenerated code. This code was generated when I added the mdb file to the project... I guess this isn't the best way to do things, am I wrong? I'll try your code later... thanks
Enrique
for a small project there is nothing workg with using these autogenerated code. The reason it cannot upate is probably because the table in the mdb file does not have a primary key. If you can modify the mdb file to include a primary key you will probably not have to add any additional code yourself. failing that have a look at http://stackoverflow.com/questions/593170/tableadapter-updating-without-a-key
dice
my table has a primary key (id)
Enrique
PS: the parameter this.connection in the SqlCommand function call ( ... , this.connection); is not recognized...
Enrique