2014-10-07 33 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(); 
} 
相关问题