Results 1 to 5 of 5

Thread: [RESOLVED] [2008] - Datagridview to multidimensional array

  1. #1

    Thread Starter
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Resolved [RESOLVED] [2008] - Datagridview to multidimensional array

    Hello

    I need some advice to accomplish this, i have a datagridview with this data:
    Column1 Column2
    item1 3
    item2 1
    item1 2
    item1 5
    item2 3
    item3 1
    item1 3

    I need an array with distinct values of column1 and the sum of the values in the second dimension of the array:
    ar(item1,13), ar(item2,4),ar(item3,1).

    I don't know how many items i will have, so i don't know the correct dimension of the array...

    I have a lot of code to do the job, but i think there is a better way...

    Thanks

  2. #2
    Addicted Member
    Join Date
    Dec 2008
    Posts
    185

    Re: [2008] - Datagridview to multidimensional array

    Is there a datatable bound to the source of the datagridview?

    If so, you can run sql like commands against it.

    i.e
    Code:
           Dim dt As New DataTable
            Dim col1Sum As Integer = dt.Compute("SUM(DISTINCT Column1)", "")

  3. #3

    Thread Starter
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: [2008] - Datagridview to multidimensional array

    No there isn't...

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2008] - Datagridview to multidimensional array

    Well, I don't think you really do want a 2D array, or at least you shouldn't. There are some, but very few, situations where 2D arrays are appropriate. They should only be used when every element is a peer. If one column represents something completely different to another column, as is the case here, the a 2D array is completely inappropriate.

    One appropriate solution would be to use a Dictionary:
    vb.net Code:
    1. Dim itemSums As New Dictionary(Of String, Integer)
    2. Dim item As String
    3.  
    4. For Each row As DataGridViewRow In Me.DataGridView1.Rows
    5.     item = CStr(row.Cells(0).Value)
    6.  
    7.     If itemSums.ContainsKey(item) Then
    8.         'Add the new value to the current subtotal.
    9.         itemSums(item) += CInt(row.Cells(1).Value)
    10.     Else
    11.         'Add the item and the first value.
    12.         itemSums.Add(item, CInt(row.Cells(1).Value))
    13.     End If
    14. Next
    Another option would be to define a type that represents an item and its corresponding value and to put instances of that type in a simple collection. In both cases you're covered for the unknown number of items because collections can grow dynamically.

    If you really must use a 2D array, like if this is a poor learning exercise, then you can create the array from the collection, because at that point you know how many elements you need.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: [2008] - Datagridview to multidimensional array

    THanks again

    I was looking for a code similar to that a long time...

    Solved

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width