【发布时间】:2016-01-24 10:41:18
【问题描述】:
我有一个数据网格视图,其中填充了 .csv 文件中的记录(大约 30k 条记录,具有 30-40 个唯一 ID - 取决于文件)。它看起来像这样:
现在我的问题是如何按 id 总结这些列?或者也许有一种方法可以将已经汇总的值直接放入datagridview?
private void dropListBox_DragDrop(object sender, DragEventArgs e)
{
data= new List<Raport>();
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (string file in files)
{
var nameOnly = System.IO.Path.GetFileName(file);
dropListBox.Items.Clear();
dropListBox.Items.Add(nameOnly);
dataGridView1.Rows.Clear();
string[] readText = File.ReadAllLines(file, Encoding.GetEncoding("Windows-1250"));
int i = 0;
foreach (string line in readText)
{
if (i++ == 0) continue;
var values = line.Split(';');
string a= values[0];
string b= values[1];
string c= values[2];
string user = values[3];
int xValues= int.Parse(values[4]);
int yValues= int.Parse(values[5]);
double d= double.Parse(values[6]);
string e= values[7];
string f= values[8];
string g= values[9];
string h= values[10];
double i= double.Parse(values[11]);
string j= values[12];
Raport Raport = new Raport(
a,
b,
c,
user,
xValues,
yValues,
d,
e,
f,
g,
h,
i,
j);
data.Add(Raport);
dataGridView1.Rows.Add(Raport.user, Raport.xValues, Raport.yValues, "8", "8");
这是我的代码。最后两列无关紧要(它总是用固定值填充)
任何建议将不胜感激。
应该是这样的
John | 5 | 4
Carl | 3 | 1
John | 1 | 6
Carl | 4 | 1
然后
John | 6 | 10
Carl | 7 | 2
报告类的结构
class Raport
{
public readonly string a;
public readonly string b;
public readonly string c;
public readonly string user;
public readonly int xValue;
public readonly int yValue;
public readonly double d;
public readonly string e;
public readonly string f;
public readonly string g;
public readonly string h;
public readonly double i;
public readonly string j;
public Raport(
string a,
string b,
string c,
string user,
int xValue,
int yValue,
double d,
string e,
string f,
string g,
string h,
double i,
string j)
{
this.a= a;
this.b= b;
this.c= c;
this.user= user;
this.xValue= xValue;
this.yValue= yValue;
this.d= d;
this.e= e;
this.f= f;
this.g= g;
this.h= h;
this.i= i;
this.j= j;
}
public Raport()
{
}
public override string ToString()
{
return a+ " ; " +
b+ " ; " +
c+ " ; " +
user+ " ; " +
xValue+ " ; " +
yValue+ " ; " +
d+ " ; " +
e+ " ; " +
f+ " ; " +
g+ " ; " +
h+ " ; " +
i+ " ; " +
j;
}
}
}
【问题讨论】:
-
这些列 'a,b,c,d,e,f,g,h,i,j' 中的哪一个是 ID? Raport 类的结构是什么?您要对哪些字段求和?
-
ID 是“用户”之一,我要总结的值是基于 ID 的 xValues 和 yValues。(我将它们放在 datagridview 中这一行:dataGridView1.Rows.Add(Raport. user, Raport.xValues, Raport.yValues, "8", "8"); ) 'a,b,c,d,e,f,g,h,i,j' 无关紧要,但存在于我放入应用程序的 .csv 文件。
标签: c# csv datagridview