Adding a new datagridview row to use default values

I have the following SQL that creates a table and inserts the first row of data without any problems. By 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 use the DataGridView and BindingNavigator controls. When I add a new line, the default values ​​are not inserted, but zeros are inserted instead. I think this has something to do with the controls, but am not sure how to get the default values ​​to be used.

Any ideas?

thanks

+3
source share
2 answers

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

Source: https://habr.com/ru/post/1720660/


All Articles