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

时间:2013-04-24 17:43:16

标签: c# mysql database datagridview

大家好日子。我有这个代码填充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);
        }

1 个答案:

答案 0 :(得分:2)

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();
        }
    }
}