【发布时间】:2012-01-12 12:19:53
【问题描述】:
您好,我在这里遇到了问题。代码如下所示。
private void Form3_Load(object sender, EventArgs e)
{
string connectionString =
"Server=localhost;" +
"Database=oroderm;" +
"User ID=root;" +
"Password=root;" +
"Pooling=false";
string query = "Select * from client";
MySqlConnection conn = new MySqlConnection(connectionString);
MySqlDataAdapter dAdapter = new MySqlDataAdapter(query, connectionString);
conn.Open();
DataSet ds = new DataSet();
MySqlCommandBuilder cBuilder = new MySqlCommandBuilder(dAdapter);
dAdapter.Fill(ds, "client");
BindingSource bSource = new BindingSource();
bSource.DataSource = ds.Tables["client"];
dataGridView2.DataSource = bSource;
}
private void button1_Click(object sender, EventArgs e)
{
DataTable dt = ds.Tables["client"];
this.dataGridView2.BindingContext[dt].EndCurrentEdit();
this.da.Update(dt);
}
所以我想要的是,每当我在我的 Datagrid 中编辑值时,它都会在我单击 button1(保存按钮)后影响我的数据库。前任。如果我在更改名称后将罗马作为客户并单击 button1,它应该会更改。但是我得到一个值不能是空错误。请有人帮忙。 T_T
*编辑:这是更新的代码
private MySqlDataAdapter _da;
private DataTable _dt;
private DataSet _ds;
private void Form3_Load(object sender, EventArgs e)
{
updateClient();
}
public void updateClient()
{
string connectionString =
"Server=localhost;" +
"Database=oroderm;" +
"User ID=root;" +
"Password=root;" +
"Pooling=false";
string query = "select * from client";
MySqlConnection conn = new MySqlConnection(connectionString);
_da = new MySqlDataAdapter(query, connectionString);
conn.Open();
_ds = new DataSet();
MySqlCommandBuilder cBuilder = new MySqlCommandBuilder(_da);
_da.Fill(_ds, "client");
BindingSource bSource = new BindingSource();
bSource.DataSource = _ds.Tables["client"];
dataGridView2.DataSource = bSource;
_da.UpdateCommand = cBuilder.GetUpdateCommand();
}
private void button1_Click(object sender, EventArgs e)
{
_dt = _ds.Tables["client"];
this.dataGridView2.BindingContext[_dt].EndCurrentEdit();
this._da.Update(_dt);
}
【问题讨论】:
标签: c# winforms datagrid datagridview editing