2
你好我在這裏有一個問題。代碼看起來像這樣。值不能爲空。參數名稱:dataSource。編輯Datagrid並保存到數據庫
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);
}
我已經成功地使通過爲數據集,datatable和mysqladapter創建全局變量來運行代碼。但是,我只能修改一次表格的內容。編輯一次後,我無法編輯我的表格的內容。這有什麼問題? –
@rjtubera:我不知道,但這聽起來像是一個單獨的問題,需要更詳細的描述和更新的代碼。 (順便說一下,你不應該需要真正的* global *變量。) –
Ive把上面的代碼放在上面。非常感謝你幫助我。 T_T –