【发布时间】:2017-06-13 08:23:33
【问题描述】:
我正在尝试使用我能理解的方式导入 CSV 并在 DataGridView 中显示数据。
这是到目前为止我理解它是如何工作的代码和解释。
如果我错过了理解,请纠正我。
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 test2
{
public partial class Form1 : Form
{
//Open the choose GUI to choose the file that we want to import
OpenFileDialog openFile = new OpenFileDialog();
public Form1()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
//if open successfully, then apply streamReader to it
if (openFile.ShowDialog() == DialogResult.OK)
{
StreamReader sr = new StreamReader(openFile.FileName);
//read the data in the file by using readLine
var rl = sr.ReadLine();
// If the rl is not null, then print (is it correct?)....
if(rl != null)
{
///code to print data
}
}
}
//filter out the csv file.
private void Form1_Load_1(object sender, EventArgs e)
{
openFile.Filter = "CSV|*.csv";
}
}
}
现在,我正在尝试打印数据。
我知道我需要使用 DataGridView.DataSource 来打印数据(如果我错了,请纠正我)但我不知道如何申请。
那么,我的解释是真的还是我有什么要补充的???
--初学者。
【问题讨论】:
-
一种方法是创建 DataTable,列应与 CSV 文件的内容匹配,并将 Datatable 分配给 Grid。
标签: c# csv datagridview