- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我想将 csv 文件读入 Datagridview。我想要一个像这样读取 csv 的类和函数:
class Import
{
public DataTable readCSV(string filePath)
{
DataTable dt = new DataTable();
using (StreamReader sr = new StreamReader(filePath))
{
string strLine = sr.ReadLine();
string[] strArray = strLine.Split(';');
foreach (string value in strArray)
{
dt.Columns.Add(value.Trim());
}
DataRow dr = dt.NewRow();
while (sr.Peek() >= 0)
{
strLine = sr.ReadLine();
strArray = strLine.Split(';');
dt.Rows.Add(strArray);
}
}
return dt;
}
}
并调用它:
Import imp = new Import();
DataTable table = imp.readCSV(filePath);
foreach(DataRow row in table.Rows)
{
dataGridView.Rows.Add(row);
}
结果是->创建了行,但单元格中没有数据!!
最佳答案
public DataTable readCSV(string filePath)
{
var dt = new DataTable();
// Creating the columns
File.ReadLines(filePath).Take(1)
.SelectMany(x => x.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
.ToList()
.ForEach(x => dt.Columns.Add(x.Trim()));
// Adding the rows
File.ReadLines(filePath).Skip(1)
.Select(x => x.Split(';'))
.ToList()
.ForEach(line => dt.Rows.Add(line));
return dt;
}
低于另一个使用 foreach 循环的版本
public DataTable readCSV(string filePath)
{
var dt = new DataTable();
// Creating the columns
foreach(var headerLine in File.ReadLines(filePath).Take(1))
{
foreach(var headerItem in headerLine.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
{
dt.Columns.Add(headerItem.Trim());
}
}
// Adding the rows
foreach(var line in File.ReadLines(filePath).Skip(1))
{
dt.Rows.Add(x.Split(';'));
}
return dt;
}
首先我们使用 File.ReadLines,它返回一个 IEnumerable,它是行的集合。我们使用 Take(1) 来获取第一行,这应该是标题,然后我们使用 SelectMany 将 Split 方法返回的字符串数组转换为一个列表,所以我们调用 ToList 现在我们可以使用 ForEach 方法在 DataTable 中添加列。
要添加行,我们仍然使用 File.ReadLines,但现在我们 Skip(1),这会跳过标题行,现在我们将使用 Select,创建一个 Collection<Collection<string>>
,然后再次调用 ToList,最后调用 ForEach 将行添加到 DataTable 中。 File.ReadLines 在 .NET 4.0 中可用。
观察: File.ReadLines不读取所有行,它返回一个 IEnumerable,并且对行进行惰性求值,因此仅第一行将被加载两次。
The ReadLines and ReadAllLines methods differ as follows: When you use ReadLines, you can start enumerating the collection of strings before the whole collection is returned; when you use ReadAllLines, you must wait for the whole array of strings be returned before you can access the array. Therefore, when you are working with very large files, ReadLines can be more efficient.
You can use the ReadLines method to do the following:
Perform LINQ to Objects queries on a file to obtain a filtered set of its lines.
Write the returned collection of lines to a file with the File.WriteAllLines(String, IEnumerable) method, or append them to an existing file with the File.AppendAllLines(String, IEnumerable) method.
Create an immediately populated instance of a collection that takes an IEnumerable collection of strings for its constructor, such as a IList or a Queue.
This method uses UTF8 for the encoding value.
如果您仍有任何疑问,请查看此答案:What is the difference between File.ReadLines() and File.ReadAllLines()?
首先,安装这个nuget包
PM> Install-Package CsvHelper
对于给定的 CSV,我们应该创建一个类来表示它
CSV 文件
Name;Age;Birthdate;Working
Alberto Monteiro;25;01/01/1990;true
Other Person;5;01/01/2010;false
类模型是
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public DateTime Birthdate { get; set; }
public bool Working { get; set; }
}
现在让我们使用 CsvReader 来构建数据表
public DataTable readCSV(string filePath)
{
var dt = new DataTable();
var csv = new CsvReader(new StreamReader(filePath));
// Creating the columns
typeof(Person).GetProperties().Select(p => p.Name).ToList().ForEach(x => dt.Columns.Add(x));
// Adding the rows
csv.GetRecords<Person>().ToList.ForEach(line => dt.Rows.Add(line.Name, line.Age, line.Birthdate, line.Working));
return dt;
}
要在 DataTable 中创建列,请使用一些反射,然后使用方法 GetRecords 在 DataTabble 中添加行
关于c# - 在 DataGridView 中读取 CSV 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34612021/
我已经看到 Datagridview 不允许复制和粘贴多个单元格的文本,是否有一个简单的设置来启用它,或者我是否必须使用键处理程序和剪贴板数据存储来包含该功能。 用户想要在一行中复制 3 个单元格,并
我有一个 DataGridView我想限制用户只为特定列下的单元格输入数值的控件。我怎样才能在 DataGridView 中完成这种类型的验证细胞? 当我创建一个简单的文本框时是可能的,但是我如何验证
我正在尝试格式化 DataGridView,使用样式颜色等。DGV 在表单启动时加载(通过 buildGrid 方法),如构造函数代码中所示: public Report1(DataSet d
第一列中的小黑三角的官方DataGridView命名法描述是什么? 好像是标记了DataGridView.CurrentRow的位置,但是只是一个get属性,我想设置一下。 Grid with arr
我有点卡在我写的一些代码上 一个大纲是我正在从 SQL 数据库中读取一些数据,并希望将其显示在表单上的 DataGridView 中。我已经确认有数据从数据库返回,但不确定为什么没有出现。我遵循了互联
我的 DataTable从数据库中提取了三列,而我只需要将其中的两列绑定(bind)到 DataGridView .你能帮我吗? 最佳答案 这可能有用 DataSet ds = new DataSet
我有一个包含 Dataset ds 和 DataGridView dgv 的 WinForms 应用程序。 dgv 绑定(bind)到 ds。 ds 通过 Task() 更新,该 Task() 使用
我有DataGridView(dgHome),在parentForm(Home)中有一些列。子 Form(bill) 有 DataGridView(dgbill) 。我需要当我单击 dgHome 中的
我有两个具有相同列架构的 DataGridView(尽管有两个不同的 DataView 作为 DataSource - 如果这很重要)。将一行从一个数据 GridView 移动到另一个数据 GridV
基本上我有 2 个 DataGridView,我需要将行从一个复制到另一个。 到目前为止我已经尝试过: DataGridViewRowCollection tmpRowCollection = Dat
我正在尝试根据它包含的行数使我的 DataGridView 的高度自动调整。目前,我能够通过以下行完成此操作: dataGridView_SearchResults.AutoSize = true;
使用 WinForms、C# .Net 2.0 我正在向非绑定(bind) DataGridView 添加行。我想在其中一列中有一个 DataGridViewButtonColumn,在单击时删除该行
我有一个 datagridview(dataGridView1,其中包含一个 dataGridview2 列标题),其中我有一个复选框列,称为 dgvCkb,在 dataGridView1。当我在任何
对不起,如果这是一个愚蠢的问题。我在这方面很陌生。我应该如何将组合框添加到数据表,然后将其加载到数据 GridView 中?这可以做到吗?这是最好的方法吗?非常感谢有关如何执行此操作的提示和教程。先感
我正在尝试将 .txt 文件导入 DataGrid。问题是虽然代码大部分工作正常,但在导入 .txt 文件时,它会创建额外的行,例如 this ; OpenFileDialog ofd
datagridview 加载速度非常慢。我该如何优化它? datagridview有4-5千行。 我必须根据几个参数动态生成一个 datagridview。(来自数据库的数据,列数) 我必须从数据库
我怎样才能强制DataGridView.CellValueChanged DataGridViewCell.Value 立即引发事件(并将更改实际提交给 ComboBox 属性)单元格中的编辑控件更改
我有一个通过数据集从数据库中获取数据的功能 public DataSet getDataSet(string query) { DataSet ds = new DataSet()
客户希望能够键入一个字母并让系统滚动 DataGridView,这样具有与字母匹配的单元格的第一行将滚动到 DataGridView 的顶部 任何建议将不胜感激 最佳答案 我不确定您将如何有效地查找与
好吧,我有一个 DataGridView,用户可以在其中单击列标题进行排序。当他们在应用排序时添加新行时,直到验证该行时才会创建记录(在退出 newRow 之前他们无法执行此操作)。排序后如何才能选择
我是一名优秀的程序员,十分优秀!