gpt4 book ai didi

sql - 添加新行 datagridview 以使用默认值

转载 作者:行者123 更新时间:2023-12-01 11:09:27 24 4
gpt4 key购买 nike

我有以下创建表并毫无问题地插入第一行数据的 SQL。默认 DateTime 值也按预期插入。

CREATE TABLE Jobs (
[Id] int PRIMARY KEY IDENTITY(1,1),
[JobName] nvarchar(256) Default 'SomeName',
[CreateDate] DateTime DEFAULT GETDATE(),
[ModifyDate] DateTime DEFAULT GETDATE(),
[LastOpenDate] DateTime DEFAULT GETDATE(),
[CreatedByUser] nvarchar(64) Default 'SomeUser',
[Title] nvarchar(256) Default 'SomeTitle')
GO
INSERT INTO Jobs (JobName)
VALUES ('NewName')
GO

在 Visual Studio 2008 中,我使用 DataGridView 和 BindingNavigator 控件。当我添加新行时,不会插入默认值,而是插入空值。我认为这与控件有关,但不确定如何获取要使用的默认值。

有什么想法吗?

谢谢

最佳答案

您可以 set the default values for the Datagridview在 DefaultValuesNeeded 事件上

例如:

private void dataGridView1_DefaultValuesNeeded(object sender,
System.Windows.Forms.DataGridViewRowEventArgs e)
{
e.Row.Cells["Region"].Value = "WA";
e.Row.Cells["City"].Value = "Redmond";
e.Row.Cells["PostalCode"].Value = "98052-6399";
e.Row.Cells["Region"].Value = "NA";
e.Row.Cells["Country"].Value = "USA";
e.Row.Cells["CustomerID"].Value = NewCustomerId();
}

关于sql - 添加新行 datagridview 以使用默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1599846/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com