C# DataGridView Deleting All Columns

Robert Karamagi 96 Reputation points
2020-11-08T20:18:59.333+00:00

After some time, when using the DataGridView in C#, the Columns are all deleted when trying to re-arrange them. When adding a new column it gives an Error that the column is not a valid identifier.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,892 questions
0 comments No comments
{count} votes

Accepted answer
  1. Robert Karamagi 96 Reputation points
    2020-11-08T20:19:19.967+00:00

    From the forums I have read, this seems to be an existing bug in Visual Studio. I recommend that you manually write out the code in your "Form.Designer.cs". You will have to enter your respective code appropriately and then your column will automatically appear in your DataGridView without the "Column is not a valid identifier" issue that appears when using the Graphical User Interface.

    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;

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.