我正在尝试允许在datagridview表中更改值
OnCellendEdit事件
using (SqlConnection conn = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand())
{
conn.Open();
cmd.Connection = conn;
cmd.CommandText = "UPDATE employee SET @selected = '@upd' WHERE id = @id";
cmd.Parameters.AddWithValue("@selected", dataGridView1.CurrentCell.OwningColumn.HeaderText);
cmd.Parameters.AddWithValue("@upd", dataGridView1.CurrentCell.Value.ToString());
cmd.Parameters.AddWithValue("@id", dataGridView1.CurrentRow.Cells[0].Value);
cmd.ExecuteNonQuery();
conn.Close();
}
}
this.employeeTableAdapter.Fill(this.testDataSet.employee);
使用上述内容构建更新查询,但只是在按Enter键后重置
@selected retrieves column name
@upd retrieves entered value
@id retrieves id number
所以最终查询应该是
UPDATE employees
SET firstName = 'john'
WHERE id = 1
但在执行后不会提交任何更改,并且值会重置为原始值。
查看了值,并已正确检索它们。