Bug #18346 DataGridView.Update() does not update
Submitted: 19 Mar 2006 22:29 Modified: 7 Apr 2006 13:00
Reporter: Peter Brawley (Basic Quality Contributor) Email Updates:
Status: Not a Bug Impact on me:
None 
Category:Connector / NET Severity:S1 (Critical)
Version:1.0.7 OS:Windows (W2K SP4)
Assigned to: CPU Architecture:Any

[19 Mar 2006 22:29] Peter Brawley
Description:
DataGridView.Update (Visual Studio 2005) method does not update MySQL tables.

How to repeat:
Create a new C# project (here called "Bug2"). Reference the NET 2.0 build of MySQL.Data.Dll. Drag two DataGridViews and a button onto Form1. Flip to code view, paste in the foillowing code. Insert values for username, password, database name, sMasterTbl, sDetailTbl and sRelationName. Run it, edit a few cells, close and re-open the app. The updates do not take.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MySql.Data;

namespace Bug2 {
  public partial class Form1 : Form {
 
    MySql.Data.MySqlClient.MySqlDataAdapter daMaster, daDetail;
    private BindingSource bindSrcMaster = new BindingSource();
    private BindingSource bindSrcDetail = new BindingSource();
    System.Data.DataSet ds;
    System.Data.DataRelation relCustOrd;
    string sConn, sMasterTbl, sDetailTbl;
  
    public Form1() {
      InitializeComponent();

      sConn = "server=127.0.0.1;uid=USR;pwd=PWD;database=DBNAME;";
      sMasterTbl = "Customers";
      sDetailTbl = "Orders";

      string sMasterKey = "customerID";
      string sDetailKey = sMasterKey;
      string sRelationName = sMasterTbl + sDetailTbl;
      string sqlMaster = "SELECT * FROM " + sMasterTbl;
      string sqlDetail = "SELECT * FROM " + sDetailTbl;
        
      MySql.Data.MySqlClient.MySqlConnection oConn;
      oConn = new MySql.Data.MySqlClient.MySqlConnection( sConn );
      oConn.Open();

      dataGridView1.DataSource = bindSrcMaster;
      dataGridView2.DataSource = bindSrcDetail;
            
      ds = new System.Data.DataSet();
 
      daMaster = new MySql.Data.MySqlClient.MySqlDataAdapter(sqlMaster, oConn);
      daMaster.Fill( ds, sMasterTbl );
      
      daDetail = new MySql.Data.MySqlClient.MySqlDataAdapter(sqlDetail, oConn);
      daDetail.Fill(ds, sDetailTbl );

      ds.Relations.Clear();
      relCustOrd = new DataRelation(
                           sRelationName,
                           ds.Tables[sMasterTbl].Columns[sMasterKey],
                           ds.Tables[sDetailTbl].Columns[sDetailKey] );
      ds.Relations.Add( relCustOrd );

      bindSrcMaster.DataSource = ds;
      bindSrcMaster.DataMember = sMasterTbl;
      bindSrcDetail.DataSource = bindSrcMaster;
      bindSrcDetail.DataMember = sRelationName;

      this.Load += new System.EventHandler( GridForm_Load );
    }
   
    private void btnUpdate_Click(object sender, System.EventArgs e) {
      daMaster.Update( ds, sMasterTbl );
      daDetail.Update( ds, sDetailTbl );
    }

    private void GridForm_Load(object sender, System.EventArgs e) {
      dataGridView1.AutoResizeColumns();
      dataGridView2.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
    }

  }
}

Suggested fix:
None known.
[30 Mar 2006 2:34] Peter Brawley
Forget this, it's my error. With appropriate MySqlCommandBuilder calls, and a call to DataSet.HasChanges(), DataGridView updates work fine.
[26 Jul 2006 8:11] alice chen
Can You tell me how to do ?
I has same problem.
e-mail:chen0929@gmail.com

Thanks