添加新列将删除datagrid视图的所有现有列

时间:2014-08-07 12:52:31

标签: c# .net winforms datagridview

我正在使用datagridview,其中我在designmode中添加了列并且它不是数据绑定。这个网格在很长一段时间内工作得很好。现在我想在datagrid视图中插入一个新列,而我不是能够添加列。如果我添加它正在删除datagridview的所有现有列。我甚至无法编辑现有列,因为同样的事情正在发生(删除datagrid视图的所有列)。我已google如此多,无法找到可能的解决方案。如果有人遇到同样的问题,请提供解决方案。 注意:我没有以编程方式添加任何行或列。它是一个使用c#.net vs2010的Windows应用程序。

感谢

3 个答案:

答案 0 :(得分:0)

如果您遇到winforms设计器问题,请尝试删除并重新添加现有控件。确保您不会更改设计器代码文件中的任何代码,除非您真的知道自己在做什么。

答案 1 :(得分:0)

当表单分成2个文件时,visual studio发生了变化:form.csform.designer.cs。我自己仍然有很多单文件表单,但它们没有问题。

我猜你已经对表格做了些什么。例如,带有参数的新构造函数。或者手动编辑InitializeComponent方法(永远不要这样做!)。如果可以的话,尽量使用设计师来反转这一点。

更多 clean 可以通过检查DataGridView方法代码,简单地将所有必须写入InitializeComponent的内容写下来,然后只需添加新的DataGridView并在设计师中再做一切。

更好的解决方案是从头开始重新设计表单。重构几年的代码是个好主意,特别是如果你有这样的问题。

答案 2 :(得分:0)

在我读过的论坛中,这似乎是Visual Studio中的现有错误。我建议您手动在“ Form.Designer.cs”中写出代码。您将必须正确输入各自的代码,然后您的列将自动出现在DataGridView中,而不会出现使用图形用户界面时出现的“列不是有效标识符”的问题。

Form.Designer.cs

#region Component Designer generated code

/// <summary> 
/// Required method for Designer support - do not modify 
/// the contents of this method with the code editor.
/// </summary>

private void InitializeComponent()
{
    this.YourColumnName = new System.Windows.Forms.DataGridViewTextBoxColumn();
    this.dataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
        this.YourColumnName
    });
    // 
    // YourColumnName
    // 
    this.YourColumnName.HeaderText = "Your Column Name";
    this.YourColumnName.Name = "YourColumnName";
    this.YourColumnName.ReadOnly = true;
    // 
}

#endregion
private System.Windows.Forms.DataGridViewTextBoxColumn YourColumnName;