“The name 'GRID' does not exist in the current context” error

  1. List item

Hey guys recently have been doing my first ever windows forms/c# application and i cannot get rid of this error. I've dragged and dropped data grid view into my form and renamed it GRID and this problem persists. Here is the code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO; //added
using System.Windows.Forms;
using System.Runtime.Serialization.Formatters.Binary; //added
using System.Runtime.Serialization; //added
namespace testowa // this my name of project
{

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void InitializeComponent()
{
    throw new NotImplementedException();
}



[Serializable] // It allow our class to be saved in file
public class data // Our class for data
{
public string name;
public string surname;
public string city;
public string number;

}



private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
{
GRID.EndEdit(); 
SaveFileDialog saveFileDialog1 = new SaveFileDialog(); //Creating a file save dialog 

saveFileDialog1.RestoreDirectory = true;
//read and filter the raw data
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream output = new FileStream(saveFileDialog1.FileName, FileMode.OpenOrCreate, FileAccess.Write);
;
int n = GRID.RowCount; 
data[] Person = new data[n - 1]; //We have as many records as many rows, rows are added automaticly so we have always one row more than we need, so n is a number of rows -1 empty row
for (int i = 0; i < n - 1; i++) 
{

Person[i] = new data();
//GRID has two numbers in"[]" first numer is an index of column, second is a an idnex of row', indexing always starts from 0'
Person[i].name = GRID[0, i].Value.ToString();
Person[i].surname = GRID[1, i].Value.ToString();
Person[i].city = GRID[2, i].Value.ToString();
Person[i].number = GRID[3, i].Value.ToString();

}

formatter.Serialize(output, Person);

output.Close();
}

}

private void OpenToolStripMenuItem_Click(object sender, EventArgs e) // Reading a File and adding data to GRID
{
openFileDialog1 = new OpenFileDialog();
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
BinaryFormatter reader = new BinaryFormatter();
FileStream input = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
data[] Person = (data[])reader.Deserialize(input);
GRID.Rows.Clear(); 
for (int i = 0; i < Person.Length; i++)
{
GRID.Rows.Add();
GRID[0, i].Value = Person[i].name;
GRID[1, i].Value = Person[i].surname;
GRID[2, i].Value = Person[i].city;
GRID[3, i].Value = Person[i].number;

}

}
}



private void CloseToolStripMenuItem_Click(object sender, EventArgs e)
{
Close(); // closing an app
}


public OpenFileDialog openFileDialog1 { get; set; }
}
}

What should i do? Thanks.

form1.designer

 namespace WindowsFormsApplication7
    {
        partial class Form1
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;

            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }

            #region Windows Form 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.menuStrip1 = new System.Windows.Forms.MenuStrip();
                this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
                this.saveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
                this.openToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
                this.closeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
                this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
                this.GRID = new System.Windows.Forms.DataGridView();
                this.Column1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
                this.Column2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
                this.Column3 = new System.Windows.Forms.DataGridViewTextBoxColumn();
                this.Column4 = new System.Windows.Forms.DataGridViewTextBoxColumn();
                this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
                this.menuStrip1.SuspendLayout();
                ((System.ComponentModel.ISupportInitialize)(this.GRID)).BeginInit();
                this.SuspendLayout();
                // 
                // menuStrip1
                // 
                this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
                this.fileToolStripMenuItem});
                this.menuStrip1.Location = new System.Drawing.Point(0, 0);
                this.menuStrip1.Name = "menuStrip1";
                this.menuStrip1.Size = new System.Drawing.Size(651, 24);
                this.menuStrip1.TabIndex = 0;
                this.menuStrip1.Text = "menuStrip1";
                // 
                // fileToolStripMenuItem
                // 
                this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
                this.saveToolStripMenuItem,
                this.openToolStripMenuItem,
                this.closeToolStripMenuItem});
                this.fileToolStripMenuItem.Name = "fileToolStripMenuItem";
                this.fileToolStripMenuItem.Size = new System.Drawing.Size(37, 20);
                this.fileToolStripMenuItem.Text = "File";
                // 
                // saveToolStripMenuItem
                // 
                this.saveToolStripMenuItem.Name = "saveToolStripMenuItem";
                this.saveToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
                this.saveToolStripMenuItem.Text = "Save";
                this.saveToolStripMenuItem.Click += new System.EventHandler(this.saveToolStripMenuItem_Click);
                // 
                // openToolStripMenuItem
                // 
                this.openToolStripMenuItem.Name = "openToolStripMenuItem";
                this.openToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
                this.openToolStripMenuItem.Text = "Open";
                this.openToolStripMenuItem.Click += new System.EventHandler(this.openToolStripMenuItem_Click);
                // 
                // closeToolStripMenuItem
                // 
                this.closeToolStripMenuItem.Name = "closeToolStripMenuItem";
                this.closeToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
                this.closeToolStripMenuItem.Text = "Close";
                this.closeToolStripMenuItem.Click += new System.EventHandler(this.closeToolStripMenuItem_Click);
                // 
                // openFileDialog1
                // 
                this.openFileDialog1.FileName = "openFileDialog1";
                this.openFileDialog1.FileOk += new System.ComponentModel.CancelEventHandler(this.openFileDialog1_FileOk);
                // 
                // GRID
                // 
                this.GRID.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
                this.GRID.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
                this.Column1,
                this.Column2,
                this.Column3,
                this.Column4});
                this.GRID.Location = new System.Drawing.Point(13, 28);
                this.GRID.Name = "GRID";
                this.GRID.Size = new System.Drawing.Size(451, 232);
                this.GRID.TabIndex = 1;
                this.GRID.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellContentClick);
                // 
                // Column1
                // 
                this.Column1.HeaderText = "name";
                this.Column1.Name = "Column1";
                // 
                // Column2
                // 
                this.Column2.HeaderText = "last name";
                this.Column2.Name = "Column2";
                // 
                // Column3
                // 
                this.Column3.HeaderText = "City";
                this.Column3.Name = "Column3";
                // 
                // Column4
                // 
                this.Column4.HeaderText = "Phone #";
                this.Column4.Name = "Column4";
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(651, 262);
                this.Controls.Add(this.GRID);
                this.Controls.Add(this.menuStrip1);
                this.MainMenuStrip = this.menuStrip1;
                this.Name = "Form1";
                this.Text = "Form1";
                this.menuStrip1.ResumeLayout(false);
                this.menuStrip1.PerformLayout();
                ((System.ComponentModel.ISupportInitialize)(this.GRID)).EndInit();
                this.ResumeLayout(false);
                this.PerformLayout();

            }

            #endregion

            private System.Windows.Forms.MenuStrip menuStrip1;
            private System.Windows.Forms.ToolStripMenuItem fileToolStripMenuItem;
            private System.Windows.Forms.ToolStripMenuItem saveToolStripMenuItem;
            private System.Windows.Forms.ToolStripMenuItem openToolStripMenuItem;
            private System.Windows.Forms.ToolStripMenuItem closeToolStripMenuItem;
            private System.Windows.Forms.OpenFileDialog openFileDialog1;
            private System.Windows.Forms.DataGridView GRID;
            private System.Windows.Forms.DataGridViewTextBoxColumn Column1;
            private System.Windows.Forms.DataGridViewTextBoxColumn Column2;
            private System.Windows.Forms.DataGridViewTextBoxColumn Column3;
            private System.Windows.Forms.DataGridViewTextBoxColumn Column4;
            private System.Windows.Forms.SaveFileDialog saveFileDialog1;
        }
    }

Answers


Looks like you have a namespace of testowa for the Form1.cs file, but WindowsApplication7 for the Form1.Designer.cs. Try matching them up.

Also remove this code snippet from your Form1.cs

private void InitializeComponent()
{
    throw new NotImplementedException();
}

And this code snippet

public OpenFileDialog openFileDialog1 { get; set; }

Also, you need to make your event handlers match what is in the designer

SaveToolStripMenuItem_Click

should be

saveToolStripMenuItem_Click

and so on and so forth.

Finally add a handlers for the openFileDialog_FileOK and CellContentClick in your Form1.cs

    void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
    {

    }

    void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {

    }

Edit

Form1.cs

using System;
using System.IO; //added
using System.Windows.Forms;
using System.Runtime.Serialization.Formatters.Binary; //added

//added
namespace testowa // this my name of project
{

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        [Serializable] // It allow our class to be saved in file
        public class data // Our class for data
        {
            public string name;
            public string surname;
            public string city;
            public string number;

        }



        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GRID.EndEdit();
            SaveFileDialog saveFileDialog1 = new SaveFileDialog(); //Creating a file save dialog 

            saveFileDialog1.RestoreDirectory = true;
            //read and filter the raw data
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                BinaryFormatter formatter = new BinaryFormatter();
                FileStream output = new FileStream(saveFileDialog1.FileName, FileMode.OpenOrCreate, FileAccess.Write);
                ;
                int n = GRID.RowCount;
                data[] Person = new data[n - 1]; //We have as many records as many rows, rows are added automaticly so we have always one row more than we need, so n is a number of rows -1 empty row
                for (int i = 0; i < n - 1; i++)
                {

                    Person[i] = new data();
                    //GRID has two numbers in"[]" first numer is an index of column, second is a an idnex of row', indexing always starts from 0'
                    Person[i].name = GRID[0, i].Value.ToString();
                    Person[i].surname = GRID[1, i].Value.ToString();
                    Person[i].city = GRID[2, i].Value.ToString();
                    Person[i].number = GRID[3, i].Value.ToString();

                }

                formatter.Serialize(output, Person);

                output.Close();
            }

        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e) // Reading a File and adding data to GRID
        {
            openFileDialog1 = new OpenFileDialog();
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                BinaryFormatter reader = new BinaryFormatter();
                FileStream input = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
                data[] Person = (data[])reader.Deserialize(input);
                GRID.Rows.Clear();
                for (int i = 0; i < Person.Length; i++)
                {
                    GRID.Rows.Add();
                    GRID[0, i].Value = Person[i].name;
                    GRID[1, i].Value = Person[i].surname;
                    GRID[2, i].Value = Person[i].city;
                    GRID[3, i].Value = Person[i].number;

                }

            }
        }



        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close(); // closing an app
        }

        void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
        {

        }

        void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}

Form1.Designer.cs

namespace testowa
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form 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.menuStrip1 = new System.Windows.Forms.MenuStrip();
            this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.saveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.openToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.closeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
            this.GRID = new System.Windows.Forms.DataGridView();
            this.Column1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Column2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Column3 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Column4 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
            this.menuStrip1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.GRID)).BeginInit();
            this.SuspendLayout();
            // 
            // menuStrip1
            // 
            this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
                this.fileToolStripMenuItem});
            this.menuStrip1.Location = new System.Drawing.Point(0, 0);
            this.menuStrip1.Name = "menuStrip1";
            this.menuStrip1.Size = new System.Drawing.Size(651, 24);
            this.menuStrip1.TabIndex = 0;
            this.menuStrip1.Text = "menuStrip1";
            // 
            // fileToolStripMenuItem
            // 
            this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
                this.saveToolStripMenuItem,
                this.openToolStripMenuItem,
                this.closeToolStripMenuItem});
            this.fileToolStripMenuItem.Name = "fileToolStripMenuItem";
            this.fileToolStripMenuItem.Size = new System.Drawing.Size(37, 20);
            this.fileToolStripMenuItem.Text = "File";
            // 
            // saveToolStripMenuItem
            // 
            this.saveToolStripMenuItem.Name = "saveToolStripMenuItem";
            this.saveToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.saveToolStripMenuItem.Text = "Save";
            this.saveToolStripMenuItem.Click += new System.EventHandler(this.saveToolStripMenuItem_Click);
            // 
            // openToolStripMenuItem
            // 
            this.openToolStripMenuItem.Name = "openToolStripMenuItem";
            this.openToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.openToolStripMenuItem.Text = "Open";
            this.openToolStripMenuItem.Click += new System.EventHandler(this.openToolStripMenuItem_Click);
            // 
            // closeToolStripMenuItem
            // 
            this.closeToolStripMenuItem.Name = "closeToolStripMenuItem";
            this.closeToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.closeToolStripMenuItem.Text = "Close";
            this.closeToolStripMenuItem.Click += new System.EventHandler(this.closeToolStripMenuItem_Click);
            // 
            // openFileDialog1
            // 
            this.openFileDialog1.FileName = "openFileDialog1";
            this.openFileDialog1.FileOk += new System.ComponentModel.CancelEventHandler(this.openFileDialog1_FileOk);
            // 
            // GRID
            // 
            this.GRID.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.GRID.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
                this.Column1,
                this.Column2,
                this.Column3,
                this.Column4});
            this.GRID.Location = new System.Drawing.Point(13, 28);
            this.GRID.Name = "GRID";
            this.GRID.Size = new System.Drawing.Size(451, 232);
            this.GRID.TabIndex = 1;
            this.GRID.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellContentClick);
            // 
            // Column1
            // 
            this.Column1.HeaderText = "name";
            this.Column1.Name = "Column1";
            // 
            // Column2
            // 
            this.Column2.HeaderText = "last name";
            this.Column2.Name = "Column2";
            // 
            // Column3
            // 
            this.Column3.HeaderText = "City";
            this.Column3.Name = "Column3";
            // 
            // Column4
            // 
            this.Column4.HeaderText = "Phone #";
            this.Column4.Name = "Column4";
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(651, 262);
            this.Controls.Add(this.GRID);
            this.Controls.Add(this.menuStrip1);
            this.MainMenuStrip = this.menuStrip1;
            this.Name = "Form1";
            this.Text = "Form1";
            this.menuStrip1.ResumeLayout(false);
            this.menuStrip1.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.GRID)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.MenuStrip menuStrip1;
        private System.Windows.Forms.ToolStripMenuItem fileToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem saveToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem openToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem closeToolStripMenuItem;
        private System.Windows.Forms.OpenFileDialog openFileDialog1;
        private System.Windows.Forms.DataGridView GRID;
        private System.Windows.Forms.DataGridViewTextBoxColumn Column1;
        private System.Windows.Forms.DataGridViewTextBoxColumn Column2;
        private System.Windows.Forms.DataGridViewTextBoxColumn Column3;
        private System.Windows.Forms.DataGridViewTextBoxColumn Column4;
        private System.Windows.Forms.SaveFileDialog saveFileDialog1;
    }
}

Program.cs

using System;
using System.Windows.Forms;
using testowa;

namespace WindowsFormsApplication1
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

Need Your Help

How to produce a time delay in JavaScript for a chat App?

javascript chat

I'm currently a beginner at JavaScript and predominantly code in Java.

git trying to convert LF to CRLF on ubuntu with core.autocrlf set to true

git configuration core.autocrlf

I'm using git on ubuntu with autocrlf set to true. My editor is configured to use LF as end-of-line character. After I edited a file and did

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.