1

I have a datagridview which is populated with records from .csv file (around 30k records with 30-40 unique id's - depends on file). It looks like this:

Now my question is how to sum up those columns by id? Or maybe there is a way to directly put already sumed up values to 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");

This is the code I have. The last 2 columns doesn't matter (It's always filled with fixed values)

Any suggestions would be appreciated.

It should go like this

John |   5   |     4    
Carl |   3   |     1  
John |   1   |     6  
Carl |   4   |     1 

then

John |   6   |  10  
Carl |   7   |   2

the structure of the Raport Class

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;
    }
}
}
Bronchi
  • 21
  • 1
  • 3
  • Which of these columns 'a,b,c,d,e,f,g,h,i,j' is the ID one? And what is the structure of the class Raport? What fields do you want to sum? – Steve Jan 24 '16 at 10:47
  • ID is the "user" one, the values I want to sum up is xValues and yValues based on the ID.(I put them in datagridview in this line: dataGridView1.Rows.Add(Raport.user, Raport.xValues, Raport.yValues, "8", "8"); ) The 'a,b,c,d,e,f,g,h,i,j' doesn't matter but the exist in .csv file which I put to the app. – Bronchi Jan 24 '16 at 10:55

1 Answers1

0

Currently you're already adding every Raport object to a list and the DataGridView:

data= new List<Raport>();
// ...
data.Add(Raport);
dataGridView1.Rows.Add(Raport.user, Raport.xValues, Raport.yValues, "8", "8");

You can utilize this list and LINQ to your advantage to group your objects by the user property, summing the other two needed properties, as seen here. This will be stored into another list, which can then be used to fill your DataGridView:

var summedData = data.GroupBy(r => new { r.user })
                     .Select(r => new Raport()
                                  {
                                      user = r.Key.user,
                                      xValue = r.Sum(innerR => innerR.xValue),
                                      yValue = r.Sum(innerR => innerR.yValue)
                                  }
                             );

Then, instead of adding each Raport created during creation of your original datalist, just loop through the new summed list:

foreach (Raport raport in summedData)
{
    dataGridView1.Rows.Add(raport.user, raport.xValues, raport.yValues, "8", "8");
}
Community
  • 1
  • 1
OhBeWise
  • 5,350
  • 3
  • 32
  • 60