Quantcast
Viewing all articles
Browse latest Browse all 12583

"The name 'GRID' does not exist in the current context" Error

Hey guys c# beginner here, i've been trying to create my first ever application in windows forms, a phonebook application and i couldn't get rid of this error. 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; }
}
}

Can you help me please?

Viewing all articles
Browse latest Browse all 12583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>