【发布时间】:2019-04-20 05:10:30
【问题描述】:
我有一个 CSV 文件,其中 A 列将根据 Image 1 包含重复值。在这个 CSV 文件中,我只希望我的 datagridview 显示来自 A 列的不同值,并按照图片 1 跳过标题为 Name 的标题列。所以我的 datagridview 应该按照 @ 显示987654322@.
但是,使用下面的代码,datagridview 显示为Image 3。下面是我的代码。我正在使用Linq 和Csvhelper。我使用了link 1 和link 2 作为参考。希望能得到帮助
1 类
public string Name
{
get; set;
}
表格 3
private void Form3_Load(object sender, EventArgs e)
{
//Create Datatable
DataSet dts = new DataSet();
DataTable dataTable = new DataTable();
dataTable.Columns.Add("Username", typeof(string));
//Read the data from the CSV file
StreamReader streamReader = new StreamReader(@"\Users\d_sim\Desktop\test.csv");
CsvReader reader = new CsvReader(streamReader);
reader.Configuration.Encoding = Encoding.UTF8;
reader.Configuration.Delimiter = ",";
//Make Class 1 into a list.
List<Class1> records = reader.GetRecords<Class2>().ToList();
//Only Read the applicable property and display in datagridview. IRL Class1 has many properties but I only include one in this question
var distinctbyproperty = records.GroupBy(x => x.Name).Select(x => x.First());
dataTable.Rows.Add(distinctbyproperty.ToString());
dts.Tables.Add(dataTable);
this.dataGridView1.DataSource = dataTable;
dataGridView1.AllowUserToAddRows = false;
}
【问题讨论】:
-
顺便问一下class1的目的是什么?你不在代码中使用它?
-
@masyita shariff 在
List<Class1> records =行中确实使用了Class1。那里有个小bug,剩下的应该是reader.GetRecords<Class1>().ToList();