2012-03-19 88 views
6

在我的Windows窗体上我有一个DataGridView组件,它绑定到一个BindingSource。 BindingSource是EntityFramework对象的对象数据源。DataGridView自动生成列

有时我的DataBridView中的列被更新。有时候所有的属性都会添加为列,但现在它也删除了我所有的列。所以我失去了我所有的设置。

何时将列自动获取添加?

(我用VS.NET 2010)

更新:

// 
// Summary: 
//  Gets or sets a value indicating whether columns are created automatically 
//  when the System.Windows.Forms.DataGridView.DataSource or System.Windows.Forms.DataGridView.DataMember 
//  properties are set. 
// 
// Returns: 
//  true if the columns should be created automatically; otherwise, false. The 
//  default is true. 
[Browsable(false)] 
[EditorBrowsable(EditorBrowsableState.Advanced)] 
[DefaultValue(true)] 
public bool AutoGenerateColumns { get; set; } 

属性没有在设计展现出来,并且 “隐藏高级属性” 未选中。

更新2: 当我更新我的实体框架模型时,将再次添加所有列。我只能在表单的构造函数中设置属性。这非常烦人。

+0

将DataGridView的[AutoGenerateColumns](http://msdn.microsoft.com/zh-cn/library/system.windows.forms.datagridview.autogeneratecolumns.aspx)属性设置为false。 – 2012-03-19 10:11:37

+0

我在窗体构造函数中设置了AutoGenerateColumns = false,但仍然出现问题 – 2013-01-05 15:41:34

回答

4

我实际上不知道什么时候发生这种情况,但我倾向于手动创建所有列。我在设计器中创建了列,并在我的代码中将AutoGenerateColumns属性设置为false

+1

非常感谢。这就是我所做的,手动设计了柱子和尺寸。该属性没有出现在设计器中,但我现在将其设置在代码中。 – Bojo 2012-03-19 10:14:01

+1

是的,我不知道为什么这个属性不能在设计器中设置 - 这是因为从Visual Studio 2005 ... – 2012-03-19 10:40:01

2

添加该代码或更改您的DataGridView属性AutoGenerateColumns

DataGridView1.AutoGenerateColumns=false; 
+1

请参阅我的问题的更新。我还无法回答问题。 – Bojo 2012-03-19 12:28:12

1

尽量离开第一个自动生成的列,并设置它的知名度假。 如果它没有帮助,请尝试使用Visible = false离开它们。 抱歉,糟糕的英语。

1

将AutoGenerateColumns属性设置为False,但请记住在数据绑定之前执行此操作。 例如: DataGridView1.AutoGenerateColumns=false; DataGridView1.DataSource=getData();

默认情况下,它被设置为True。