开发者

Adding new row datagridview to use default values

I have the following SQL that creates a table and inserts the first row of data without a problem. Th开发者_如何学Goe default DateTime values are also inserted as expected.

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

In Visual Studio 2008 I am using the DataGridView and BindingNavigator controls. When I add a new row the default values are not inserted but nulls are inserted instead. I think this is something to do with the controls but not sure how to get the default values to be used.

Any ideas?

Thanks


You can set the default values for the Datagridview on the DefaultValuesNeeded event

eg:

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();
} 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜