Amna Ahmed
Amna Ahmed

Reputation: 1964

DatagridView Changes Do not reflect in Database

I have a datagridview, which is set to EditMode : EditOnKeystroke. When i edit the data in grid and click an update button, the changes do not reflect in the database table which is the source of data for this grid. The code that loads the dataGridView and updates it is given below.

private void Form_Load(object sender, EventArgs e)
{                
      // TODO: This line of code loads data into the 'pFSDataSet5.Farms' table. You can move, or remove it, as needed.
      this.farmsTableAdapter.Fill(this.pFSDataSet5.Farms);    
}

private void update_Click(object sender, EventArgs e)
{
      //da.Update((DataTable)farmsTableAdapter);
      int s=this.farmsTableAdapter.Update(this.pFSDataSet5.Farms);
      Console.Write(s);

      MessageBox.Show("Successfully Updated");
}

What have i missed, that data changes do not propogate to db table called 'Farms' ?

Upvotes: 2

Views: 2074

Answers (2)

MORFEE89
MORFEE89

Reputation: 147

The changes not reflect in your database, beacuse you don't close editing datagridview . Use this 2 lines to close datagridview editing:

 yourdatagridview.EndEdit();
 ((DataRowView)yourdatagridview.CurrentRow.DataBoundItem).EndEdit();

Then you can use the tableadapter update. :)

Upvotes: 2

Ianthe the Duke of Nukem
Ianthe the Duke of Nukem

Reputation: 1761

Changes don't propagate to the database because there is no binding between the data grid and the dataset.

Try setting the grid.DataSource with the dataset table on form load.

Upvotes: 0

Related Questions