试图弄清楚为啥我的表单的数据网格只保存前 35 行

Posted

技术标签:

【中文标题】试图弄清楚为啥我的表单的数据网格只保存前 35 行【英文标题】:trying to figure out why my form's datagrid only saves the first 35 rows试图弄清楚为什么我的表单的数据网格只保存前 35 行 【发布时间】:2016-11-09 16:02:27 【问题描述】:

我的 windows 窗体加载一个 csv 文件并在数据网格中显示数据。但是当我单击保存按钮时,它只保存网格中的前 35 行,并切断其余行。无论我添加多少数据,它总是在同一个位置中断

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication19

    public partial class WoodStock : Form
    
        public WoodStock()
        
            InitializeComponent();
        

        private void Form1_Load(object sender, EventArgs e)
        
            //Opens csv file and splits the data seperated by a ',' into cells
            System.IO.StreamReader file = new System.IO.StreamReader(@"C:\StockFile\Stocklist.csv");
            string[] columnnames = file.ReadLine().Split(',');
            DataTable dt = new DataTable();
            foreach (string c in columnnames)
            
                dt.Columns.Add(c);

            

            string newline;
            while ((newline = file.ReadLine()) != null)
            
                DataRow dr = dt.NewRow();
                string[] values = newline.Split(',');
                for (int i = 0; i < values.Length; i++)
                
                    dr[i] = values[i];
                
                dt.Rows.Add(dr);
            

            file.Close();
            dataGridView1.DataSource = dt;
            
        

        

        private void saveToolStripButton_Click(object sender, EventArgs e)
        
            MessageBox.Show("Data Saved.");
            System.IO.StreamWriter fileWriter = new StreamWriter(@"C:\StockFile\stocklist.csv");
            
            string columnHeaderText = "";

            //Collecting DataGridView Column Header Text
            int countColumn = dataGridView1.ColumnCount - 1;

            if (countColumn >= 0)
            
                columnHeaderText = dataGridView1.Columns[0].HeaderText;
            

            for (int i = 1; i <= countColumn; i++)
            
                columnHeaderText = columnHeaderText + ',' + dataGridView1.Columns[i].HeaderText;
            

            //Writing Column Header Text in File
            fileWriter.WriteLine(columnHeaderText);

            //Collecting Data Rows
            foreach (DataGridViewRow dataRowObject in dataGridView1.Rows)
            
                //Checking for New Row in DataGridView 
                if (!dataRowObject.IsNewRow)
                
                    string dataFromGrid = "";

                    dataFromGrid = dataRowObject.Cells[0].Value.ToString();

                    for (int i = 1; i <= countColumn; i++)
                    
                        dataFromGrid = dataFromGrid + ',' + dataRowObject.Cells[i].Value.ToString();
                    

                    //Writing Data Rows in File         
                    fileWriter.WriteLine(dataFromGrid);
                    
                
                
            
        
    

【问题讨论】:

原始文档有多少行? 也许你只有 35 行不是新的? csv 文件中有 40+ 行不包括标题 【参考方案1】:

你的问题是你没有关闭StreamWriter,所以不仅它保持文件锁打开,它也没有正确地将缓冲区刷新到文件中。因为它是IDisposable,所以有一个更好的方法来编写这段代码:

private void Form1_Load(object sender, EventArgs e)

    //Opens csv file and splits the data seperated by a ',' into cells
    using (StreamReader file = new StreamReader(@"..."))
    
        string[] columnnames = file.ReadLine().Split(',');
        DataTable dt = new DataTable();
        foreach (string c in columnnames)
        
            dt.Columns.Add(c);

        

        string newline;
        while ((newline = file.ReadLine()) != null)
        
            DataRow dr = dt.NewRow();
            string[] values = newline.Split(',');
            for (int i = 0; i < values.Length; i++)
            
                dr[i] = values[i];
            
            dt.Rows.Add(dr);
        

        dataGridView1.DataSource = dt;
    


private void saveToolStripButton_Click(object sender, EventArgs e)

    MessageBox.Show("Data Saved.");

    using (StreamWriter fileWriter = new StreamWriter(@"..."))
    
        string columnHeaderText = "";

        //Collecting DataGridView Column Header Text
        int countColumn = dataGridView1.ColumnCount - 1;

        if (countColumn >= 0)
        
            columnHeaderText = dataGridView1.Columns[0].HeaderText;
        

        for (int i = 1; i <= countColumn; i++)
        
            columnHeaderText = columnHeaderText + ',' + dataGridView1.Columns[i].HeaderText;
        

        //Writing Column Header Text in File
        fileWriter.WriteLine(columnHeaderText);

        //Collecting Data Rows
        foreach (DataGridViewRow dataRowObject in dataGridView1.Rows)
        
            //Checking for New Row in DataGridView 
            if (!dataRowObject.IsNewRow)
            
                string dataFromGrid = "";

                dataFromGrid = dataRowObject.Cells[0].Value.ToString();

                for (int i = 1; i <= countColumn; i++)
                
                    dataFromGrid = dataFromGrid + ',' + dataRowObject.Cells[i].Value.ToString();
                

                //Writing Data Rows in File         
                fileWriter.WriteLine(dataFromGrid);

            

        
    

确保将实际文件名放回去,我使用stocklist.csvstocklist2.csv让他们看看发生了什么。

现在您无需手动调用file.Close();,它会在using 块的末尾自动调用。

【讨论】:

以上是关于试图弄清楚为啥我的表单的数据网格只保存前 35 行的主要内容,如果未能解决你的问题,请参考以下文章

无法弄清楚为啥我在我的 jsonp 序列化中得到重复数据

试图弄清楚为啥 NewID 函数停止在 Oracle 上工作

为啥我的光子双关语代码返回“null”?

ADO.net执行多个查询(ExecuteQuery和ExecuteScalar)并在Web表单控件中显示结果

当我写下原因时,我的 AFK 命令只显示一个字。为啥?

如何在C#中将列表与WPF数据网格绑定?