Results 1 to 4 of 4

Thread: [RESOLVED] 2 dimensional array question

  1. #1

    Thread Starter
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,556

    Resolved [RESOLVED] 2 dimensional array question

    Hi Guys,

    I have a gridview in my asp.net website that and I use vb for the code behind. Each time a row is bound an even called the Gridview_RowDatabound event is fired. So what I am doing is looping through all the cells in that row:

    Code:
        Private ArrDisbursements(,) As String = New String(,) {}
    
     For c As Integer = 4 To e.Row.Cells.Count - 1
                   ReDim ArrDisbursements(e.Row.RowIndex, c - 4)
                    For Each ctl As Control In e.Row.Cells(c).Controls
                        If TypeOf ctl Is TextBox Then
                            Dim txtbox As TextBox = CType(ctl, TextBox)
    
                           ' ArrDisbursements(e.Row.RowIndex, c - 4) = txtbox.Text
                                             End If
                    Next
    
                Next
    Problem here is I need to store Row,Column pair values like this, say there are 2 rows with 3 columns each :

    so when row 1 is bound my array must be:

    ArrDisbursements(0,1) = 74
    ArrDisbursements(0,2) = 75
    ArrDisbursements(0,3) = 76

    then when row 2 is bound, the above values must be preserved and the next row values must be added:

    ArrDisbursements(1,1) = 77
    ArrDisbursements(1,2) = 78
    ArrDisbursements(1,3) = 79

    Problem is I get an error saying redim can only change the rightmost dimension. It would suit my needs to change both dimensions here as my final result will be to total each column in the array. I posted in this forum since it's an array question please help. what other method can i use if arrays don't suit this task

  2. #2

    Thread Starter
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,556

    Re: 2 dimensional array question

    ok I figured one way to do this was to create a class to hold the rowIndex, columnIndex and the value I need:

    Code:
    Private Class CostingRowColumn
            Private m_RowIndex As Integer
            Private m_ColumnIndex As Integer
            Private m_Value As Decimal
    
            Public Sub New(ByVal row As Integer, ByVal column As Integer, ByVal value As Decimal)
                m_RowIndex = row
                m_ColumnIndex = column
                m_Value = value
            End Sub
    
            Public Property RowIndex() As Integer
                Get
                    Return m_RowIndex
                End Get
                Set(ByVal value As Integer)
                    m_RowIndex = value
                End Set
            End Property
    
            Public Property ColumnIndex() As Integer
                Get
                    Return m_ColumnIndex
                End Get
                Set(ByVal value As Integer)
                    m_ColumnIndex = value
                End Set
            End Property
    
            Public Property Value() As Decimal
                Get
                    Return m_Value
                End Get
                Set(ByVal value As Decimal)
                    m_Value = value
                End Set
            End Property
        End Class
    then I create a List :

    Code:
       Private DisbursementList As New List(Of CostingRowColumn)
    and in the rowdatabound event I add objects to the list:

    Code:
    DisbursementList.Add(New CostingRowColumn(e.Row.RowIndex, j, Convert.ToDecimal(txtbox.Text)))
    Now I need help with a LINQ query that will return a total of the Value Properties grouped by the columnindex.

    So say I have these values

    Code:
    RowIndex 1 ColumnIndex 1 Value 74
    RowIndex 2 CoumnIndex 1  Value 75
    RowIndex 3 ColumnIndex 1 Value 76
    
    RowIndex 1 ColumnIndex 2 Value 10
    RowIndex 2 ColumnIndex 2 Value 20
    RowIndex 3 ColumnIndex 2 Value 30
    I must get a Total of 225 for column 1 and a total of 60 for column 2.

  3. #3

    Thread Starter
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,556

    Re: 2 dimensional array question

    this seems to work please can someone let me know if there is a better way:

    Code:
    Dim q = From p In DisbursementList.AsEnumerable _
                Group p By p.ColumnIndex Into Sum(p.Value) _
                Select ColumnIndex, ColSum = Sum

  4. #4

    Thread Starter
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,556

    Re: 2 dimensional array question

    guess I'll stick with this since it does the job. wouldn't mind alternative solutions though

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