C#自学37一手动输入到dataGridView数据存储到txt文本,清空数据,读取txt文本

private void bntSave_Click(object sender, EventArgs e)
        {
            //实例化一个保存文件对话框
            SaveFileDialog sf = new SaveFileDialog();
            //设置文件保存类型
            sf.Filter = "txt文件|*.txt";
            //如果用户没有输入扩展名,自动追加后缀
            sf.AddExtension = true;
            //设置标题
            sf.Title = "写文件";
            //如果用户点击了保存按钮
            if (sf.ShowDialog() == DialogResult.OK)
            {
                //实例化一个文件流--->与写入文件相关联
                FileStream fs = new FileStream(sf.FileName, FileMode.Create);
                //实例化一个StreamWriter-->与fs相关联
                StreamWriter sw = new StreamWriter(fs);
                //开始写入
                if (this.dataGridView1.Rows.Count < 1)
                {
                    MessageBox.Show("没有数据!导出失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    //因为DataGridView最后一行为空,所以减一
                    // sw.WriteLine(this.dataGridView1.Rows.Count - 1);
                    for (int i = 0; i < this.dataGridView1.Rows.Count - 1; i++)
                    {
                        //如果某一列数据为空,就写入"",因为空对象不能调用tostring();
                        if (this.dataGridView1.Rows[i].Cells[0].Value != null)
                            sw.WriteLine("数学" + "=" + this.dataGridView1.Rows[i].Cells[0].Value.ToString());
                        else
                            sw.WriteLine("");

                        if (this.dataGridView1.Rows[i].Cells[1].Value != null)
                            sw.WriteLine(this.dataGridView1.Rows[i].Cells[1].Value.ToString());
                        else
                            sw.WriteLine("");

                        if (this.dataGridView1.Rows[i].Cells[2].Value != null)
                            sw.WriteLine(this.dataGridView1.Rows[i].Cells[2].Value.ToString());
                        else
                            sw.WriteLine("");

                        if (this.dataGridView1.Rows[i].Cells[3].Value != null)
                            sw.WriteLine(this.dataGridView1.Rows[i].Cells[3].Value.ToString());
                        else
                            sw.WriteLine("");

                        if (this.dataGridView1.Rows[i].Cells[4].Value != null)
                            sw.WriteLine(this.dataGridView1.Rows[i].Cells[4].Value.ToString());
                        else
                            sw.WriteLine("");
                        //if (this.dataGridView1.Rows[i].Cells[5].Value != null)
                        //    sw.WriteLine(this.dataGridView1.Rows[i].Cells[5].Value.ToString());
                        //else
                        //    sw.WriteLine("");
                        //if (this.dataGridView1.Rows[i].Cells[6].Value != null)
                        //    sw.WriteLine(this.dataGridView1.Rows[i].Cells[6].Value.ToString());
                        //else
                        //    sw.WriteLine("");
                        //if (this.dataGridView1.Rows[i].Cells[7].Value != null)
                        //    sw.WriteLine(this.dataGridView1.Rows[i].Cells[7].Value.ToString());
                        //else
                        //    sw.WriteLine("");
                        //if (this.dataGridView1.Rows[i].Cells[8].Value != null)
                        //    sw.WriteLine(this.dataGridView1.Rows[i].Cells[8].Value.ToString());
                        //else
                        sw.WriteLine("");
                    }
                    //写入
                    //sw.WriteLine(name);
                    //sw.WriteLine(number);
                    //sw.WriteLine(Chinese);
                    //sw.WriteLine(Math);
                    //sw.WriteLine(English);
                    //清空缓冲区
                    sw.Flush();
                    //关闭流
                    sw.Close();
                    fs.Close();
                    MessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
private void bntclear_Click(object sender, EventArgs e)
        {
            if (dataGridView1.DataSource != null)
            {
                DataTable dt = (DataTable)dataGridView1.DataSource;
                dt.Rows.Clear();
                dataGridView1.DataSource = dt;
            }
            else
            {
                dataGridView1.Rows.Clear();
            }
        }
private void bntRead_Click(object sender, EventArgs e)
        {
            //读取文件前先清除内容
            for (int i = 0; i < this.dataGridView1.Rows.Count - 1; i++)
            {
                for (int j = 0; j < dataGridView1.ColumnCount; j++)
                {
                    this.dataGridView1.Rows[i].Cells[j].Value = null;
                }
            }
            //实例化一个保存文件对话框
            OpenFileDialog sf = new OpenFileDialog();
            //设置文件保存类型
            sf.Filter = "txt文件|*.txt";
            //如果用户没有输入扩展名,自动追加后缀
            sf.AddExtension = true;
            //设置标题
            sf.Title = "读文件";
            //如果用户点击了保存按钮
            if (sf.ShowDialog() == DialogResult.OK)
            {
                //实例化一个文件流--->与写入文件相关联
                FileStream fs = new FileStream(sf.FileName, FileMode.Open);
                //实例化一个StreamWriter-->与fs相关联
                StreamReader sw = new StreamReader(fs);
                //开始读取
                if (this.dataGridView1.Rows.Count < 1)
                {
                    MessageBox.Show("没有数据!读取失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    int RowCount = int.Parse(sw.ReadLine());
                    //判断如果当前行数大于要读取的行数,就不add行,否则add行
                    if (RowCount <= this.dataGridView1.Rows.Count)//|| RowCount == this.dataGridView1.Rows.Count
                    {
                        for (int i = 0; i < RowCount; i++)
                        {
                            for (int j = 0; j < dataGridView1.ColumnCount; j++)
                            {
                                this.dataGridView1.Rows[i].Cells[j].Value = sw.ReadLine();
                            }
                        }
                    }
                    else
                    {
                        for (int i = 0; i < RowCount; i++)
                        {
                            this.dataGridView1.Rows.Add();
                            for (int j = 0; j < dataGridView1.ColumnCount; j++)
                            {
                                this.dataGridView1.Rows[i].Cells[j].Value = sw.ReadLine();
                            }
                        }
                    }
                    //关闭流
                    sw.Close();
                    fs.Close();
                    MessageBox.Show("读取成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

你可能感兴趣的:(C#零基础自学,c#)