Datagridview单元格值更改更新数据库

我已经将Mysql数据库中的数据检索到DataGridView1中。 让我们假设我在第0行。当我更改第0行,单元格1的内容并按回车键或按钮时,更新查询应修改该行,但我无法修改单元格的值。 当我重新加载数据并且未修改数据库时,单元格保持其先前的值。 例如,如果我将Client_Name列下的单元格内容从“Acs”更改为“Gmt”,如何将单元格的值从“Acs”更改为“Gmt”? 并将其更新到Mysql数据库中,我在Vs 2012中使用c#。下面是我的代码,它将我的数据库检索到datagridview1,欢迎任何帮助谢谢。

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using MySql.Data; using MySql.Data.MySqlClient; using System.Configuration; using System.Data.SqlClient; namespace PI.Gen { public partial class frmMain : Form { MySqlConnection Conn; public frmMain() { InitializeComponent(); btnDisconnect.Enabled = true; btnLoadData.Enabled = false; btnLoadClients.Enabled = false; } private void btnConnect_Click(object sender, EventArgs e) { string strConnect = "server=" + txtServer.Text + ";uid=" + txtUsername.Text + ";pwd=" + txtPassword.Text + ";database=" + txtDatabase.Text; try { if (txtServer.TextLength <= 0 || txtUsername.TextLength <= 0 || txtDatabase.TextLength <= 0) { MessageBox.Show("You have an empty database connection field. Please supply a valid value."); return; } Conn = new MySqlConnection(strConnect); Conn.Open(); if (Conn.State.ToString() != "Open") { MessageBox.Show("Could not open database connection"); return; } btnDisconnect.Enabled = true; btnConnect.Enabled = false; btnLoadData.Enabled = true; btnLoadClients.Enabled = true; // btnSubmitClient.Enabled = true; } catch (Exception ex) // catch on general exceptions, not specific { MessageBox.Show(ex.Message); return; } } private void frmMain_FormClosing(object sender, FormClosingEventArgs e) { if (Conn != null) { Conn.Close(); } } private void btnDisconnect_Click(object sender, EventArgs e) { try { Conn.Close(); Conn = null; btnDisconnect.Enabled = false; btnConnect.Enabled = true; btnLoadData.Enabled = false; btnLoadClients.Enabled = false; } catch (Exception ex) { MessageBox.Show(ex.Message); return; } } private void btnLoadData_Click(object sender, EventArgs e) { try { string CmdString = "SELECT * FROM t_receipients"; MySqlDataAdapter sda = new MySqlDataAdapter(CmdString, Conn); DataSet ds = new DataSet(); sda.Fill(ds); dataGridView1.DataSource = ds.Tables[0].DefaultView; } catch (Exception ex) { MessageBox.Show(ex.Message); return; } } private void btnLoadClients_Click(object sender, EventArgs e) { try { string CmdString = "SELECT * FROM t_clients"; MySqlDataAdapter sda = new MySqlDataAdapter(CmdString, Conn); DataSet ds = new DataSet(); sda.Fill(ds); dataGridView1.DataSource = ds.Tables[0].DefaultView; } catch (Exception ex) { MessageBox.Show(ex.Message); return; } } 

经过一系列的试验和错误,我终于找到了我正在寻找的东西,因此能够从datagridview更新数据库下面是我的工作代码100%希望它有助于未来的人,并感谢@RageComplex提供帮助,但还有一件事是否有人知道如何实现我的意思是,而不是点击输入按钮在datagridview中进行更改,而是单击按钮ty

  private void dataGridView1_RowValidated(object sender, DataGridViewCellEventArgs e) { try { DataTable changes = ((DataTable)dataGridView1.DataSource).GetChanges(); if (changes != null) { MySqlCommandBuilder mcb = new MySqlCommandBuilder(mySqlDataAdapter); mySqlDataAdapter.UpdateCommand = mcb.GetUpdateCommand(); mySqlDataAdapter.Update(changes); ((DataTable)dataGridView1.DataSource).AcceptChanges(); MessageBox.Show("Cell Updated"); return; } } catch (Exception ex) { MessageBox.Show(ex.Message); } } 

您没有更新对数据库的更改。 保持连接打开并不意味着这会自动更新您的数据。

首先,不要保持连接打开。 在你的应用程序中,你有一个connect按钮,它有利于测试,但不是真正保持连接打开,而不是我认为的数据库。

加载数据的方式是正确的。

您为datagridview提供了一个DataSource,它是DataSet中的一个表。 因此,datagridview中所做的更改将保存到DataSet中,而不会保存到数据库中。

这是您更新数据库的方式

  public void UpdateTable(DataSet ds) { using (MySqlConnection connect = new MySqlConnection(ConnString)) { connect.Open(); MySqlDataAdapter adapt = new MySqlDataAdapter(); MySqlCommandBuilder commbuilder = new MySqlCommandBuilder(adapt); adapt.SelectCommand = new MySqlCommand("SELECT * FROM t_receipients", connect); adapt.Update(ds.Tables[0]); } } 

在更新数据库之前,请确保使用datagridview1.EndEdit()

此外,您using ,这将确保在完成该代码后再次关闭连接,最好是始终在try-except中使用它。

你一直在努力连接数据库,因为它似乎是在赞扬。 我也忘了在上面包含MySqlDataAdapter,在这种情况下我全局使用了一个适配器。 我不想将此问题报告为重复,但现在看起来有点像这样的答案。

我想提供我在我的应用程序中测试过的代码。我将它用于按钮点击事件。

 private void button3_Click(object sender, EventArgs e) { string StrQuery; try { string MyConnection2 = "server=localhost;user id=root;password=;database=k"; using (MySqlConnection conn = new MySqlConnection(MyConnection2)) { using (MySqlCommand comm = new MySqlCommand()) { comm.Connection = conn; conn.Open(); for (int i = 0; i < dataGridView3.Rows.Count; i++) { StrQuery = @"update s set Quantity='" + dataGridView3.Rows[i].Cells["Quantity"].Value.ToString() + "' where No='" + dataGridView3.Rows[i].Cells["Item No"].Value.ToString() + "';"; comm.CommandText = StrQuery; comm.ExecuteNonQuery(); } } } } catch { } } 

我想这可能对你有所帮助