【问题标题】:C# Datagridview how to edit cell back to databaseC# Datagridview 如何将单元格编辑回数据库
【发布时间】:2013-04-24 17:43:15
【问题描述】:

祝大家有美好的一天。我有这个填充 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# mysql database datagridview


    【解决方案1】:

    我的表

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

    【讨论】:

      猜你喜欢
      • 2015-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-25
      • 2011-10-25
      • 1970-01-01
      相关资源
      最近更新 更多