![](/img/trans.png)
[英]DataGridView: How to change cell color depending on data source value
[英]How to update changes in the data source after changing the cell value in the dataGridView?
用戶對單元格進行更改。
CellEndEdit
事件被觸發。
在Save ()
方法中,改變顯示在adapter.Update (table_2);
線。
更改不會顯示在數據庫中(更改不會保存到數據庫中)。
如果用戶使用按鈕調用Save ()
方法,則更改將顯示在數據庫中(在數據源表dataGridView
)。
題。
更改dataGridView中的單元格值后如何更新數據源中的更改?
我的代碼
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
i++;
Save();
richTextBox1.Text += i + " Event - `CellEndEdit` \r\n";
}
public DataTable CreateCmds()
{
table_2 = new DataTable();
try
{
string connectionString = @"Data Source=.\SQLEXPRESS1;Initial Catalog=Prb;Integrated Security=True";
string queryString = "SELECT * FROM tbl_01_Groups";
using (SqlConnection connection = new SqlConnection(connectionString))
{
adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand(queryString, connection);
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
connection.Open();
adapter.Fill(table_2);
}
}
catch (Exception ex)
{
string s = ex.Message;
string t = ex.StackTrace;
// throw;
}
return table_2;
}
public void Save()
{
string connectionString = @"Data Source=.\SQLEXPRESS1;Initial Catalog=Prb;Integrated Security=True";
string queryString = "SELECT * FROM tbl_01_Groups";
using (SqlConnection connection = new SqlConnection(connectionString))
{
adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand(queryString, connection);
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
connection.Open();
adapter.Update(table_2);
}
}
更新_1
當我使用CellValueChanged
事件時會發生同樣的事情。
CellValueChanged
事件被觸發。
在Save ()
方法中,改變顯示在adapter.Update (table_2);
線。
更改不會顯示在數據庫中(更改不會保存到數據庫中)。
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
i++;
Save();
richTextBox1.Text += i + " Event - `CellValueChanged` \r\n";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.