我有以下创建表并毫无问题地插入第一行数据的 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/