There is a DataGridView in my form, and I have a save button. Both the DataAdapter and the DataSet are automatically generated. I want to use DataAdapter.Update() to update my database, but it seems nothing changed after I updated the DataGridView when I open the table in .mdf or generate the solution again.
I knew this was asked and read some posts, trying to find the solutions but it doesn't work.
- I have set the .mdf file property 'Copy to output directory' to 'Copy if newer'
- BindingSource and BindingNavigator work successfully.
Code Sample
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.myTableTableAdapter.Fill(this.myDatabaseDataSet.myTable);
SqlCommandBuilder sqlCommandBuilder = new SqlCommandBuilder(myTableTableAdapter.Adapter);
myTableTableAdapter.Adapter.InsertCommand = sqlCommandBuilder.GetInsertCommand();
myTableTableAdapter.Adapter.DeleteCommand = sqlCommandBuilder.GetDeleteCommand();
myTableTableAdapter.Adapter.UpdateCommand = sqlCommandBuilder.GetUpdateCommand();
}
private void SaveSToolStripButton_Click(object sender, EventArgs e)
{
try
{
bindingSource1.EndEdit();
myTableTableAdapter.Adapter.Update(myDatabaseDataSet.myTable);
MessageBox.Show("Succeed");
}
catch (Exception err)
{
MessageBox.Show(err.Message, "Failed");
}
}
}
}