【发布时间】:2018-02-20 10:41:05
【问题描述】:
我有一个 dataGridView,我可以在其中插入、删除和更新值,但有些事情困扰着我。 用户只能修改显示的 2 列(大小和数量),隐藏其他 2 列(ID 和 selectedComponent)。 ID是我表中的PK。
这是我为新行设置 ID 和 selectedComponent 所做的:
private void dataGridViewStock_DefaultValueNeeded(object sender,
System.Windows.Forms.DataGridViewRowEventArgs e)
{
e.Row.Cells["id"].Value = "1";
e.Row.Cells["codeArticleComponent"].Value = labelComponentChosen.Text;
}
无论我为 ID 设置什么值,第一个可用的数字都会被插入到数据库中。它可以工作,但我担心它以后可能会导致错误。
有没有更好的方法来实现这一点?或者我可以保持原样吗?
【问题讨论】:
-
使用
Identity列。 -
@RezaAghaei 我已经将 ID 列声明为 Identity 并自动递增 1。
-
制作代理客户端ID。保存到数据库后用实际 id 替换它们。
标签: c# sql-server winforms