我有一个代码,可以将数据加载到网格视图中,并允许用户编辑和更新表。
为此,我“强迫”自己绕过Adapter实例。
如何避免这种情况,所以这2个功能只会无效?
public static MySqlDataAdapter LoadMySQLData(DataGridView dgv, string RequestTableQuery)
{
MySqlConnection privateConnection = new MySqlConnection(ConnectionString);
MySqlDataAdapter adapter = new MySqlDataAdapter(RequestTableQuery, privateConnection);
DataTable dt = new DataTable();
adapter.Fill(dt);
BindingSource bs = new BindingSource { DataSource = dt };
dgv.DataSource = bs;
return adapter;
}
public static void SaveMySQLData(DataGridView dgv, MySqlDataAdapter adapter)
{
BindingSource bs = dgv.DataSource as BindingSource;
bs.EndEdit();
adapter.Update(bs.DataSource as DataTable);
}