日期:2014-05-16  浏览次数:20586 次

更新 DataGridView 中修改过的Cell值到数据库中
//此为MYSQL数据库样例,MSSQL数据库只需修改数据库链接关键字等即可!
DataSet ds = new DataSet();
MySqlDataAdapter da;
private void Form1_Load(object sender, EventArgs e)
{            
	MySqlConnection cn = new MySqlConnection();
	cn.ConnectionString = @"Data Source=.;Initial Catalog=Sample;Integrated Security=True"; 
	da = newMySqlDataAdapter("SELECT 报表名称,报表金额 FROM 财务信息表", cn);
	da.Fill (ds,"Table"); 
	dataGridView1.DataSource = ds.Tables["Table"]; 
}
//更新数据的按钮 
private void button1_Click(object sender, EventArgs e) 
{ 
   try { 
	MySqlCommandBuilder com = new MySqlCommandBuilder(da); 
	da.Update(ds.Tables["Table"]); 
	MessageBox.Show("数据更新成功!"); 
    } 
    catch (Exception ex) 
    { 
	MessageBox.Show("数据更新失败!"); 
    } 
}