c# Datagridview如何编辑单元格回数据库

本文关键字:单元格 数据库 编辑 Datagridview 何编辑 | 更新日期: 2023-09-27 18:10:56

大家好。我有这个填充Datagridview的代码。我试着编辑它。但似乎我不能保存任何更改到数据库。虽然我没有得到任何错误。任何帮助都将非常感激。谢谢很多!

private void FrmViewCustomer_Load(object sender, EventArgs e)
        {
            string query = "SELECT CONCAT(firstname,', ',lastname) AS NAME, orderedgood AS OrderedGood FROM customer c;";
            using (MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["default"].ConnectionString))
            {
                conn.Open();
                using (MySqlCommand command = new MySqlCommand(query, conn))
                {
                    using (adapter = new MySqlDataAdapter(command))
                    {
                        dataGridView1.Rows.Clear();
                        dataGridView1.AllowUserToAddRows = false;
                        DataTable dt = new DataTable();
                        adapter.Fill(dt);
                        dataGridView1.DataSource = dt;
                    }
                }
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            this.adapter.Update(dt);
        }

c# Datagridview如何编辑单元格回数据库

MyTable

id   name   
1    John
2    Carl
3    Sam
c#背后的代码:
public partial class Form1 : Form
{
    DataTable dt = null;
    DataGridView dgv = null;
    public Form1()
    {
        InitializeComponent();
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        dt = new DataTable();
        using (MySqlConnection conn = new MySqlConnection("server=localhost;user=root;pwd=1234;database=test;"))
        {
            conn.Open();
            using (MySqlCommand cmd = new MySqlCommand())
            {
                cmd.Connection = conn;
                cmd.CommandText = "select * from MyTable;";
                MySqlDataAdapter da = new MySqlDataAdapter(cmd);
                da.Fill(dt);
            }
            conn.Close();
        }
        dgv = new DataGridView();
        dgv.AllowUserToAddRows = false;
        dgv.CellEndEdit += new DataGridViewCellEventHandler(dgv_CellEndEdit);
        dgv.CellValidating += new DataGridViewCellValidatingEventHandler(dgv_CellValidating);
        dgv.Dock = DockStyle.Fill;
        dgv.DataSource = dt;
        this.Controls.Add(dgv);
    }
    void dgv_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
    {
        if (e.ColumnIndex == 0)
        {
            dgv.CancelEdit();
        }
    }
    void dgv_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        string id = dt.Rows[e.RowIndex]["id"] + "";
        string col = dt.Columns[e.ColumnIndex].ColumnName;
        string data = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].Value+"";
        string sql = string.Format("UPDATE `MyTable` SET `{0}` = '{1}' WHERE ID = {2};", col, data, id);
        using (MySqlConnection conn = new MySqlConnection("server=localhost;user=root;pwd=1234;database=test;"))
        {
            conn.Open();
            using (MySqlCommand cmd = new MySqlCommand())
            {
                cmd.Connection = conn;
                cmd.CommandText = sql;
                cmd.ExecuteNonQuery();
            }
            conn.Close();
        }
    }
}