2009-10-21 100 views
3

我有以下SQL創建一個表並插入第一行數據沒有問題。默認的DateTime值也按預期插入。添加新行datagridview使用默認值

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控件。當我添加一個新行時,不插入默認值,而是插入空值。我認爲這是與控件有關的事情,但不知道如何獲得使用的默認值。

任何想法?

感謝

+0

是否經由插入數據源或其他方法gy? – solairaja 2009-10-21 10:25:02

+0

我正在使用數據源進行添加。 – Belliez 2009-10-22 14:04:48

回答

1

請參考此鏈接一些好的結果

http://forums.asp.net/p/1003755/1328318.aspx#1328318

+0

不是我正在尋找的東西,但是我通過在數據集設計器中創建一個SQL語句來實現我想要的目的,該語句將插入一個帶有單個「標題」值的新行,然後該行的其餘部分將成爲默認值。不幸的是不能這樣做使用DataGrid。謝謝 – Belliez 2009-11-18 10:50:24

+0

當你接受答案時,請投票 – solairaja 2009-12-13 12:08:28

5

您在DefaultValuesNeeded事件中爲可set the default values for the Datagridview

如:

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