我已经可以编辑数据库中的数据了。但是我遇到了一个问题,当我Quantity
在datagridview中编辑和更新列时,它应该Total
基于更新datagridview中的列Quantity * SubTotal
,但它只更新Quantity
列,而不是价格。
我哪里做错了?
这是屏幕截图:
正如您在行“1”中看到的那样,Quantity
是 100,并且SubTotal
是 10000,因此Total
将是 1000000。但是在行“2”中,Quantity
在我在 datagridview 中编辑之前,数量是100
,也是Total
2000000,当我将其更改Quantity
为 500 时,Total
仍然是 2000000,Total
应该是 10000000。
我哪里做错了?
这是代码(我根据需要发布代码):
private void Updated(object sender, EventArgs e)
{
DataTable _dt = (DataTable)dataGridView1.DataSource;
if (_dt.DefaultView.Count > 0)
{
int rowNum = dataGridView1.CurrentRow.Index;
string productCode = Convert.ToString(_dt.DefaultView[rowNum]["ProductCode"]);
int quantity = Convert.ToInt32(_dt.DefaultView[rowNum]["Quantity"]);
int price = Convert.ToInt32(_dt.DefaultView[rowNum]["SubTotal"]);
int _price = Convert.ToInt32(_dt.DefaultView[rowNum]["Total"]);
using (OleDbConnection conn = new OleDbConnection(connectionString))
{
string _commandSelect = "SELECT [Quantity], [SubTotal], [Total] FROM [TransRecord] WHERE [ProductCode] = @ProductCode";
string commandUpdate = "UPDATE [Record] SET [Quantity] = @Quantity, [SubTotal] = @SubTotal, [Total] = @Total WHERE [ProductCode] = @ProductCode";
string _commandUpdate = "UPDATE [TransRecord] SET [Quantity] = @Quantity, [SubTotal] = @SubTotal, [Total] = @Total WHERE [ProductCode] = @ProductCode";
conn.Open();
using (OleDbCommand cmdUpdate = new OleDbCommand(_commandUpdate, conn))
using (OleDbCommand _cmdSelect = new OleDbCommand(_commandSelect, conn))
using (OleDbCommand _cmdUpdate = new OleDbCommand(commandUpdate, conn))
{
_cmdSelect.Parameters.Add("@ProductCode", System.Data.OleDb.OleDbType.VarChar);
_cmdSelect.Parameters["@ProductCode"].Value = productCode;
cmdUpdate.Parameters.Add("@Quantity", System.Data.OleDb.OleDbType.Integer);
_cmdUpdate.Parameters.Add("@Quantity", System.Data.OleDb.OleDbType.Integer);
cmdUpdate.Parameters.Add("@SubTotal", System.Data.OleDb.OleDbType.Integer);
_cmdUpdate.Parameters.Add("@SubTotal", System.Data.OleDb.OleDbType.Integer);
cmdUpdate.Parameters.Add("@Total", System.Data.OleDb.OleDbType.Integer);
_cmdUpdate.Parameters.Add("@Total", System.Data.OleDb.OleDbType.Integer);
using (OleDbDataReader dReader = _cmdSelect.ExecuteReader())
{
while (dReader.Read())
{
cmdUpdate.Parameters.Add("@ProductCode", System.Data.OleDb.OleDbType.VarChar);
_cmdUpdate.Parameters.Add("@ProductCode", System.Data.OleDb.OleDbType.VarChar);
cmdUpdate.Parameters["@ProductCode"].Value = productCode;
cmdUpdate.Parameters["@Quantity"].Value = quantity;
cmdUpdate.Parameters["@SubTotal"].Value = price;
cmdUpdate.Parameters["@Total"].Value = _price;
_cmdUpdate.Parameters["@ProductCode"].Value = productCode;
_cmdUpdate.Parameters["@Quantity"].Value = quantity;
_cmdUpdate.Parameters["@SubTotal"].Value = price;
_cmdUpdate.Parameters["@Total"].Value = _price;
int numberOfRows = _cmdUpdate.ExecuteNonQuery();
int _numberOfRows = cmdUpdate.ExecuteNonQuery();
}
dReader.Close();
}
}
conn.Close();
}
}
private void UpdatePrice(object sender, EventArgs e)
{
if (numericTextBox1.TextLength >= 6)
{
decimal quantity = Convert.ToInt32(this.numericUpDown1.Value);
decimal price = Convert.ToDecimal(this.numericTextBox2.Text);
int total = Convert.ToInt32(quantity * price);
if (numericUpDown1.Value > 0)
{
this.numericTextBox3.Text = total.ToString();
}
}
}
当Total
它不在datagridview中时它正在工作,它在这里(如屏幕截图所示):
但是当它进入datagridview并且当我更改时Quantity
,它不会更新Total
基于SubTotal * Quantity
感谢您的帮助和回答!
非常感谢!