首页 > 解决方案 > C#需要帮助实时修改datagridview

问题描述

我正在尝试使 datagridview 可编辑且实时。我将存储过程内置到 mysql 中,并且假设以下代码加载该过程并运行它。问题是;没有错误,但数据根本没有被保存。当我修改一个单元格时,它会弹出成功更新的消息框,但没有保存数据。任何帮助深表感谢。

    private void Gdata_CellValueChanged(object sender, DataGridViewCellEventArgs e)
   {


        if(Gdata.CurrentRow != null)
        {
            MySqlConnection sqlCon = new MySqlConnection(MyConString);
        try 
        
            {
                sqlCon.Open();
                DataGridViewRow GDataRow = Gdata.CurrentRow;
                MySqlCommand cmd = new MySqlCommand("SalesAddOrEdit", sqlCon); //load stored procedure

                cmd.CommandType = CommandType.StoredProcedure;

                MessageBox.Show("Successfully updated!");

                cmd.Parameters.AddWithValue("d_uid", Convert.ToInt32(GDataRow.Cells["txtUID"].Value));
                cmd.Parameters.AddWithValue("d_prodid", GDataRow.Cells["txtprodid"].Value == DBNull.Value ? "" : GDataRow.Cells["txtprodid"].Value.ToString());
                cmd.Parameters.AddWithValue("d_datepurchase", GDataRow.Cells["txtdate"].Value == DBNull.Value ? "" : GDataRow.Cells["txtdate"].Value.ToString());
                cmd.Parameters.AddWithValue("d_price", GDataRow.Cells["txtprice"].Value == DBNull.Value ? "" : GDataRow.Cells["txtprice"].Value.ToString());


                cmd.Parameters.AddWithValue("d_customer_1", GDataRow.Cells["txtcus1"].Value == DBNull.Value ? "" : GDataRow.Cells["txtcus1"].Value.ToString());
                cmd.Parameters.AddWithValue("d_paid_1", GDataRow.Cells["txtpaid1"].Value == DBNull.Value ? "" : GDataRow.Cells["txtpaid1"].Value.ToString());
                cmd.Parameters.AddWithValue("d_shipped_1", GDataRow.Cells["txtship1"].Value == DBNull.Value ? "" : GDataRow.Cells["txtship1"].Value.ToString());

                cmd.Parameters.AddWithValue("d_customer_2", GDataRow.Cells["txtcus2"].Value == DBNull.Value ? "" : GDataRow.Cells["txtcus2"].Value.ToString());
                cmd.Parameters.AddWithValue("d_paid_2", GDataRow.Cells["txtpaid2"].Value == DBNull.Value ? "" : GDataRow.Cells["txtpaid2"].Value.ToString());
                cmd.Parameters.AddWithValue("d_shipped_2", GDataRow.Cells["txtship2"].Value == DBNull.Value ? "" : GDataRow.Cells["txtship2"].Value.ToString());

                cmd.ExecuteNonQuery();
                    sqlCon.Close();
                    //
                //Note for reader:
                //variables from procedure: d_(xxxx) example d_uid
                //column names(This is not the header) in datagridview: txt(xxxx) example: txtprice


                loaddata(); //once the cell updated, refresh the table to display the new changes.
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                
            }
        }
    }

标签: c#mysql

解决方案


推荐阅读