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.