2014-10-07 28 views
0

如何在c#中爲datagridview列單元格設置默認值,而某些列用數據庫值填充,一些列用像這樣的默認值填充 ..
例如第1列,第2列中,從數據庫中的值和剩餘的列第4列細胞填充第三列被填充有上午9點和第五列與下午6點如何將默認值設置爲c#中的datagridview列單元格

private void dataGridView1_DefaultValuesNeeded(object sender, 
               DataGridViewRowEventArgs e) 
{ 
    // This is not working as I explain below 
    e.Row.Cells["in_time"].Value = "9 AM"; 
    e.Row.Cells["Out_time"].Value = "6 PM"; 
} 

我使用這個代碼,但這些值是在行和列的結尾是datagridview的「In_Tim」列,並且在我單擊特定單元格之前這些值是不可見的。
如何在所有列單元格中填充默認值?

+1

[DefaultValuesNeeded](http://msdn.microsoft.com/en-us/library/ system.windows.forms.datagridview.defaultvalues需要%28v = vs.110%29.aspx)僅在點擊新行時使用 - 「在用戶輸入新記錄的行時發生,以便可以使用默認值填充「。 - 所以它不會應用於列中的所有單元格。 – stuartd 2014-10-07 11:56:05

+0

好的,但我應該用什麼來代替'DefaultValuesNeeded'來解決我的問題@stuartd – 2014-10-07 11:59:00

+0

您需要顯示代碼在哪裏填充數據庫中的值。 – stuartd 2014-10-07 12:23:28

回答

1
dataGridView1.Columns["in_time"].DefaultCellStyle.NullValue = "9 AM"; 
dataGridView1.Columns["Out_time"].DefaultCellStyle.NullValue = "6 PM"; 

一些建議後 這就是我想在DataGridView列

+1

這是空值,表示它在單元格值爲空時顯示,並且不是真正的默認值。 – 2016-01-19 15:14:55

2

DataGridView控件有一個名爲DefaultValuesNeeded它處理默認值用戶羣中新添加的行活動以顯示。

MSDN article regarding this event

的文章中列出的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["Country"].Value = "USA"; 
    e.Row.Cells["CustomerID"].Value = NewCustomerId(); 
} 
相關問題