0

I have a datagridview that contains around 21000 rows, it has the following columns "Ref","Bestandsart" and an other 20 columns each one contains a stock value in a specific week, each week a new column will be added automatically.

the issue is that some value in "Ref" are repeated but the don't necessary have the same stock values, so what i wanted to do is to go throw each row(j) and if i find a "Ref" in rows(j+1 to dg.rowcount-1) i take the stock values of that row and add it to the stock values of row(j) and remove the duplicated "Ref".

here is the code :

    Dim a = datagridview1.RowCount


    Dim s(t2 - t1)



    do until index >=a 
    If Not datagridview1.Rows(index).Cells("ref").Value.Equals(0) Then

        For i = 0 To t2 - t1
            s(i) = datagridview1.Rows(index).Cells("KW" + Convert.ToString(t1 + i)).Value
        Next

        For t = index + 1 To datagridview1.RowCount - 1
            If datagridview1.Rows(index).Cells("Ref").Value.Equals(datagridview1.Rows(t).Cells("Ref").Value) And datagridview1.Rows(index).Cells("Bestandsart").Value.Equals(datagridview1.Rows(t).Cells("Bestandsart").Value) Then
                For i = 0 To t2 - t1
                    s(i) = s(i) + datagridview1.Rows(t).Cells("KW" + Convert.ToString(t1 + i)).Value
                Next
            End If
        Next
        For i = 0 To t2 - t1
            datagridview1.Rows(index).Cells("KW" + Convert.ToString(t1 + i)).Value = s(i)

        Next

        For t = index + 1 To datagridview1.RowCount - 1
            If datagridview1.Rows(index).Cells("ref").Value.Equals(datagridview1.Rows(t).Cells("Ref").Value) And datagridview1.Rows(index).Cells("Bestandsart").Value.Equals(datagridview1.Rows(t).Cells("Bestandsart").Value) Then
                datagridview1.Rows(t).Cells("ref").Value = 0
                datagridview1.Rows(t).Cells("Bestandsart").Value = 0
            End If
        Next

        BackgroundWorker1.ReportProgress((index + 1) * 100 / a)
        index = index + 1

    Else
        BackgroundWorker1.ReportProgress((index + 1) * 100 / a)
        index = index + 1
    End If
  loop

t1 is the 1st week and t2 is the last.

the problem is that i get an out of memory exception once i reach the 10000th row or so.

any ideas on how to solve this issue.

Thank you in advance.

Chris Dunaway
  • 10,974
  • 4
  • 36
  • 48
ilyes
  • 9
  • 2
  • 1
    Run your program as x64 to create more than 10k user objects – Sasha Jun 23 '17 at 13:15
  • 1
    That's a lot of rows. Consider paginating your data. (C# examples for the general idea: [1](https://stackoverflow.com/q/2825771/3773066), [2](https://stackoverflow.com/a/29995385/3773066)) – OhBeWise Jun 23 '17 at 13:33
  • 1
    ... or rethink your GUI. Why would you show that much data to an end user? – LarsTech Jun 23 '17 at 13:46
  • I'd bet this grid is filled from a database somewhere. Further, I'd expect it's possible to re-write this to happen in a single SQL statement entirely on the DB. – Joel Coehoorn Jun 23 '17 at 13:49
  • actually it's not for an end user, sorry I forgot to mention that. am trying to use the datagridview as a training set for a neural network am working on. and yes it's filled from an excel file. – ilyes Jun 23 '17 at 14:06
  • 1
    Datagridview is a visual component, which makes a poor fit for this. You want a memory-only construct, like a DataSet, which will be far more efficient. – Joel Coehoorn Jun 23 '17 at 14:07

1 Answers1

0

I'm not sure the information you provided is enough. What version of .NET you are using? is this a Webform app or Winform.

Also where the exact exception is happening. Any inner exceptions?

Seems like you are dealing with large amount of rows. In the last 2 years I have been moving code from DataGridView To the GridView. My first suggestion is if you can , use the GridView. My experience is it's faster and less buggy and definitely much less coding. Hopefully you are Not sentenced to deal with .Net 1.1 DataGridview, which I was working on.

Also Try enabling the DataGirdView VirtualMode which is designed for cases with large amount of Data.

 dataGridView1.VirtualMode = true;

https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.virtualmode.aspx

S Nash
  • 2,363
  • 3
  • 34
  • 64
  • am using the version 4.5, it's a winform. unfortunately am not familiar with the virtual mode so i was trying to avoid it as much as possible. thank you for your reply. – ilyes Jun 23 '17 at 14:11
  • Then Do something far simpler. Use GridView. converting DataGridView to GRidView is very simple. Let me know if you have any questions. – S Nash Jun 23 '17 at 18:00