如何在使用 OpenFileDialogue 获取的 csv 文件中插入更新和删除记录作为 datagridview 中的数据源

怀胡拉迈纳

我正在尝试提出一个解决方案来读取 CSV 文件并从打开的文件对话框中编辑它们

我能够读取文件并将其显示到数据网格视图,但只能从数据网格编辑或更新,不能使用文本框和按钮。

        OpenFileDialog dialog = new OpenFileDialog();
        dialog.Filter = "CSV Files (*.csv)|*.csv";
        if (dialog.ShowDialog() == DialogResult.OK)
        {
            DataTable table = CSVReader.ReadCSVFile(dialog.FileName, true);
            dataGridView1.DataSource = table;
        }

协助编辑 CSV 文件的语法

在我完全理解你的要求之前,我不得不阅读这个问题几次。据我了解,你想在一个DataGridView中进行Insert、Update、Delete操作,然后将结果写入CSV文件,对吧。你不想直接对CSV文件进行操作,对吧。澄清后,请尝试下面的脚本并反馈。

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

namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            txtFile.Text = openFileDialog1.FileName;
            BindData(txtFile.Text);
        }

        private void BindData(string filePath)
        {
            DataTable dt = new DataTable();
            string[] lines = System.IO.File.ReadAllLines(filePath);
            if (lines.Length > 0)
            {
                //first line to create header
                string firstLine = lines[0];
                string[] headerLabels = firstLine.Split(',');
                foreach (string headerWord in headerLabels)
                {
                    dt.Columns.Add(new DataColumn(headerWord));
                }
                //For Data
                for (int i = 1; i < lines.Length; i++)
                {
                    string[] dataWords = lines[i].Split(',');
                    DataRow dr = dt.NewRow();
                    int columnIndex = 0;
                    foreach (string headerWord in headerLabels)
                    {
                        dr[headerWord] = dataWords[columnIndex++];
                    }
                    dt.Rows.Add(dr);
                }
            }
            if (dt.Rows.Count > 0)
            {
                dataGridView1.DataSource = dt;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                //Build the CSV file data as a Comma separated string.
                string csv = string.Empty;

                //Add the Header row for CSV file.
                foreach (DataGridViewColumn column in dataGridView1.Columns)
                {
                    csv += column.HeaderText + ',';
                }
                //Add new line.
                csv += "\r\n";

                //Adding the Rows

                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    foreach (DataGridViewCell cell in row.Cells)
                    {
                        if (cell.Value != null)
                        {
                            //Add the Data rows.
                            csv += cell.Value.ToString().TrimEnd(',').Replace(",", ";") + ',';
                        }
                        // break;
                    }
                    //Add new line.
                    csv += "\r\n";
                }

                //Exporting to CSV.
                string folderPath = "C:\\Users\\Excel\\Desktop\\";
                if (!Directory.Exists(folderPath))
                {
                    Directory.CreateDirectory(folderPath);
                }
                File.WriteAllText(folderPath + "test.csv", csv);
                MessageBox.Show("");
            }
            catch
            {
                MessageBox.Show("");
            }
        }
    }
}

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

Related 相关文章

热门标签

归档