Hi Jerry_lth,
You will need to use a DataAdapter and call its Update method to write back to the database. When you call the Update method, the DataAdapter analyzes the changes that have been made and executes the appropriate command (INSERT, UPDATE, or DELETE). When the DataAdapter encounters a change to a DataRow, it uses the InsertCommand, UpdateCommand, or DeleteCommand to process the change.
More information, check this page: http://msdn2.microsoft.com/en-us/library/33y2221y(VS.80).aspx
I write a simple sample for your information.
Code Block
System.Data.SqlClient;
NewDGV
public partial class Form35 : Form
{
public Form35()
{
InitializeComponent();
}
SqlConnection cn = new SqlConnection("Data Source=localhost;Initial Catalog=NorthWind;Integrated Security=True");
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
private void Form35_Load(object sender, EventArgs e)
{
SqlCommand cm = new SqlCommand("select RegionID,RegionDescription from Region", cn);
da.SelectCommand = cm;
SqlCommandBuilder cmbuilder = new SqlCommandBuilder(da); da.Fill(dt);
this.dataGridView1.DataSource = dt;
}
private void btnSave_Click(object sender, EventArgs e)
{
this.dataGridView1.BindingContext[dt].EndCurrentEdit();
this.da.Update(dt);
}
}
Hope this helps. Best regards. Rong-Chun Zhang |