0
  1. 项目清单

嘿,伙计们最近一直在做我的第一个 Windows 窗体/C# 应用程序,我无法摆脱这个错误。我已将数据网格视图拖放到我的表单中并将其重命名为 GRID,但此问题仍然存在。这是代码:

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; }
}
}

我应该怎么办?谢谢。

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;
        }
    }
4

1 回答 1

0

看起来您有一个testowa用于 Form1.cs 文件的命名空间,但 WindowsApplication7 用于 Form1.Designer.cs。尝试匹配它们。

还要从您的 Form1.cs 中删除此代码段

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

而这个代码片段

public OpenFileDialog openFileDialog1 { get; set; }

此外,您需要使您的事件处理程序与设计器中的内容相匹配

SaveToolStripMenuItem_Click

应该

saveToolStripMenuItem_Click

等等等等。

最后在 Form1.cs 中为openFileDialog_FileOKand添加一个处理程序CellContentClick

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

    }

    void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {

    }

编辑

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;
    }
}

程序.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());
        }
    }
}
于 2013-05-02T18:09:01.523 回答