c# - 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);
}
}
}
解决方案
推荐阅读
- mysql - 如何在 redbeanphp 中更新表
- javascript - 旋转横幅,但一个横幅是 html
- javascript - 无法单击基于 JavaScript 的链接,该链接也是通过 VBA 使用 Web 元素的网页上表格的一部分
- python - python 的 SimpleHTTPServer do_GET 和 do_POST 函数是如何工作的?
- sql - 如何列出SQL中多列中值出现的次数?
- javascript - 提交多个 html 表单
- c++ - C++ 构造函数和右值
- sql-server - 选择列表中只能指定单个表达式,MSSQL查询
- heroku - React-BoilerPlate 部署到 heroku 成功但应用程序错误
- visual-studio - Service Fabric 应用程序模板在 Visual Studio 中不可用