我有一个datagridview,它绑定到一个数据库。当我单击其中一个单元格以更新其值时,实际发生的是该特定单元格的值已更新,但同一行的值也更新为null。我该怎么做才能使同一行的值不更新为null但它仍保持原样?
我的代码:
int quantityColumnIndex = 0;
string particularColumnIndex = "";
int rateColumnIndex = 0;
private void buttonUpdate_Click(object sender, EventArgs e)
{
{
string SQL = "Update OrderLineItems set Quantity = " + quantityColumnIndex +
", Particular = '" + particularColumnIndex +
"', Rate = " + rateColumnIndex + " where OrderLineItems.Id = " + Id;
result = dm.ExecuteActionQuery(SQL);
}
if (result > 0)
{
MessageBox.Show("Record Updated Successfully", "Update Record", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void dataGridViewOrderDetails_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 1)
{
quantityColumnIndex = Convert.ToInt32(dataGridViewOrderDetails.Rows[e.RowIndex].Cells[e.ColumnIndex].Value);
}
if (e.ColumnIndex == 2)
{
particularColumnIndex = dataGridViewOrderDetails.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
}
if (e.ColumnIndex == 3)
{
rateColumnIndex = Convert.ToInt32(dataGridViewOrderDetails.Rows[e.RowIndex].Cells[e.ColumnIndex].Value);
}
}
需要帮助
答案 0 :(得分:2)
一种解决方案是将quantityColumnIndex,specificColumnIndex和rateColumnIndex更改为nullables并更改查询,以便它不会更新未更改的列。
我想你知道正在编辑的行是哪一行。如果不这样做,那么如果用户更改值但选择另一行,则会遇到麻烦。所以,如果您知道该行,我认为更好的选择是将查询更改为:
var row = dataGridViewOrderDetails.Rows[rowNumber];
string SQL = "Update OrderLineItems set Quantity = " + Convert.ToInt32(row.Cells[1].Value); + ", Particular = '" + row.Cells[2].Value.ToString() + "', Rate = " + Convert.ToInt32(row.Cells[e.ColumnIndex].Value)+ " where OrderLineItems.Id = " + Id;
result = dm.ExecuteActionQuery(SQL);
答案 1 :(得分:1)
您是否尝试过分配新值?
if (e.ColumnIndex == 1)
{
dataGridViewOrderDetails.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = 5;
}
但是,如果datagrid绑定到数据表,则必须更新数据表中的值。
答案 2 :(得分:1)
为什么要将Value
属性的结果转换为DataGridViewCell
类型? Value
property为您提供单元格的值,而不是单元格本身。代码错了;确保您了解DataGridView
控件的对象模型。
请尝试使用以下代码:
if (e.ColumnIndex == 1)
{
// Get a reference to the cell of interest.
DataGridViewCell quantity = dataGridViewOrderDetails.Rows[e.RowIndex].Cells[e.ColumnIndex];
// Update its value.
quantity.Value = 100; // or whatever your new value is
}
答案 3 :(得分:0)
您是否可以检查数据库中是否发生了同样的事情(将所有值更改为null,除了您编辑的值)。
在运行时检查变量的值(quantityColumnIndex,specificColumnIndex和rateColumnIndex)“result = dm.ExecuteActionQuery(SQL);”。
我认为除了刚刚编辑的值之外,所有值都将为null,因为您在dataGridViewOrderDetails_CellValueChanged(..)方法中只更改了一个变量的值。因此,您需要为dataGridViewOrderDetails_CellValueChanged函数中的所有变量赋值。
答案 4 :(得分:0)
private void buttonUpdate_Click(object sender, EventArgs e)
{
if ((dataGridViewOrderDetails.Rows.Count - 1 > RowCount) == false)
{
for (int i = 0; i < dataGridViewOrderDetails.RowCount - 1; i++)
{
string SQL = "Update OrderLineItems set Quantity = " + dataGridViewOrderDetails.Rows[i].Cells[1].Value +
", Particular = '" + dataGridViewOrderDetails.Rows[i].Cells[2].Value + "', Rate = " + dataGridViewOrderDetails.Rows[i].Cells[3].Value + " where OrderLineItems.Id = " + dataGridViewOrderDetails.Rows[i].Cells[0].Value;
result = dm.ExecuteActionQuery(SQL);
}
}
查询中存在问题。这已经解决了:))