C# &引用;名称';网格';“在当前上下文中不存在”;错误 列表项
嘿,伙计们最近一直在做我的第一个windows窗体/c应用程序,我无法摆脱这个错误。我将数据网格视图拖放到表单中,并将其重命名为网格,但问题仍然存在。代码如下:C# &引用;名称';网格';“在当前上下文中不存在”;错误 列表项,c#,windows,forms,C#,Windows,Forms,嘿,伙计们最近一直在做我的第一个windows窗体/c应用程序,我无法摆脱这个错误。我将数据网格视图拖放到表单中,并将其重命名为网格,但问题仍然存在。代码如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.
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; }
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.IO//补充
使用System.Windows.Forms;
使用System.Runtime.Serialization.Formatters.Binary//补充
使用System.Runtime.Serialization//补充
名称空间testowa//这是我的项目名称
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
私有void InitializeComponent()
{
抛出新的NotImplementedException();
}
[Serializable]//它允许我们的类保存在文件中
公共类数据//我们的类获取数据
{
公共字符串名称;
公共字符串姓氏;
公共字符串城市;
公共字符串号;
}
私有void SaveToolStripMenuItem\u单击(对象发送者,事件参数e)
{
GRID.EndEdit();
SaveFileDialog saveFileDialog1=新建SaveFileDialog();//创建文件保存对话框
saveFileDialog1.RestoreDirectory=true;
//读取并过滤原始数据
if(saveFileDialog1.ShowDialog()==DialogResult.OK)
{
BinaryFormatter formatter=新的BinaryFormatter();
FileStream output=新FileStream(saveFileDialog1.FileName、FileMode.OpenOrCreate、FileAccess.Write);
;
int n=GRID.RowCount;
data[]Person=new data[n-1];//我们的记录数和行数一样多,行是自动添加的,因此我们总是比需要的多出一行,因此n是行数-1空行
对于(int i=0;i
我该怎么办?谢谢
表格1.设计师
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;
}
}
命名空间窗口窗体应用程序7
{
部分类Form1
{
///
///必需的设计器变量。
///
private System.ComponentModel.IContainer components=null;
///
///清理所有正在使用的资源。
///
///如果应释放托管资源,则为true;否则为false。
受保护的覆盖无效处置(布尔处置)
{
if(处理和(组件!=null))
{
组件。Dispose();
}
基地。处置(处置);
}
#区域Windows窗体设计器生成的代码
///
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
///
私有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=新系统.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();
这个.SuspendLayout();
//
//菜单第1条
//
this.menuStrip1.Items.AddRange(新System.Windows.Forms.ToolStripItem[]{
此文件名为.fileToolStripMenuItem});
this.menuStrip1.Location=新系统.Drawing.Point(0,0);
this.menuStrip1.Name=“menuStrip1”;
this.menuStrip1.Size=新系统.Drawing.Size(651,24);
this.menuStrip1.TabIndex=0;
this.menuStrip1.Text=“menuStrip1”;
//
//fileToolStripMenuItem
//
this.fileToolStripMenuItem.DropDownItems.AddRange(新的System.Windows.Forms.ToolStripItem[]{
此.saveToolStripMenuItem,
此.openToolStripMenuItem,
此参数为.closeToolStripMenuItem});
此.fileToolStripMenu
private void InitializeComponent()
{
throw new NotImplementedException();
}
public OpenFileDialog openFileDialog1 { get; set; }
SaveToolStripMenuItem_Click
saveToolStripMenuItem_Click
void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
}
void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
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)
{
}
}
}
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;
}
}
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());
}
}
}