简体   繁体   中英

Adding rows to a dataGridView dynamically

VB.NET 4.0 framework Windows Forms Application. So I have a DataGridView that I have dropped on my form in designer, set all the columns to readOnly, AllowUserToAddRows = False , AllowUserToDeleteRows = False . Now for the part where it the code is going bad at.

My function Looks Like this:

    Private Sub fill_items()
    Dim prop As List(Of property_info) = db.property_info.ToList
    Dim units As List(Of unit) = db.units.ToList
    Dim _year As Integer = System.DateTime.Now.Year
    Dim fin As List(Of financial) = db.financials.Where(Function(f) f.Year_start.Value.Year = _year).OrderBy(Function(f) f.transaction_date).ToList

    Dim x As Integer = 0

    For Each _i In prop

        x += 1
        Dim _typeName As String = String.Empty
        Dim i As property_info = _i
        Select Case i.property_type
            Case Is = 0
                _typeName = "Storage"
            Case Is = 1
                _typeName = "House/Apartment"
            Case Is = 2
                _typeName = "Office Space"
        End Select

        reports1GridView.Rows.Add(_typeName, i.property_Name, " ", " ", " ", " ")

        For Each _t In units.Where(Function(f) f.propertyId = i.idProperties)
            Dim t As unit = _t
            x += 1
            For Each trans In fin.Where(Function(F) F.Unit_finId = t.UnitId)
                x += 1
                Dim _ttype As String = String.Empty
                Dim _typeCheck As Integer = 0
                Select Case trans.transaction_type
                    Case Is = 0
                        _ttype = "Payment Recieved"
                        _typeCheck = 0
                    Case Is = 2
                        _ttype = "Rent Charged"
                        _typeCheck = 1
                    Case Is = 3
                        _ttype = "Deposit"
                        _typeCheck = 1
                    Case Is = 20
                        _ttype = "Late Fee"
                        _typeCheck = 0
                    Case Is = 4
                        _ttype = "Auction Collection"
                        _typeCheck = 0
                    Case Is = 5
                        _ttype = "Auction Fee"
                        _typeCheck = 2
                    Case Is = 6
                        _ttype = "City Tax"
                        _typeCheck = 0
                    Case Is = 7
                        _ttype = "County Tax"
                        _typeCheck = 0
                    Case Is = 8
                        _ttype = "State Tax"
                        _typeCheck = 0
                    Case Is = 9
                        _ttype = "Maintenance"
                        _typeCheck = 2
                End Select

                Dim _TypeValue As Decimal = Nothing
                Select Case _typeCheck
                    Case Is = 0
                        _TypeValue = trans.Amount_Paid
                    Case Is = 1
                        _TypeValue = trans.amount_due
                    Case Is = 2
                End Select
                Dim _tDate As Date = trans.transaction_date
                Dim _tDateString As String = _tDate.ToShortDateString.ToString
                reports1GridView.Rows.Add(" ", " ", t.UnitId, _ttype, _tDateString, _TypeValue)

                Dim xl As String = String.Empty
            Next
        Next
    Next
End Sub

My problem is that the datagridview is displaying only values in the 0,1,2,3 columns of the gridview.. The Gridview looks correct until it Gets to column 3 which is where the transaction type goes. For some reason the amount that should be in column 5 is being displayed in that column and columns 4 and 5 are being left completely blank.. I looked at the values contained in the variables in the last reports1GridView.Rows.Add of the function and all of the variables are not only correct but in the correct order. So my question is why is this failing...

Supposing that your DataGridView is unbound (meaning that no columns are automatically defined) you need to create the appropriate columns required by your code. Then the Row.Add(item, ....) will work

For example:

Private Sub SetupGrid()
    reports1GridView.ColumnCount = 5
    reports1GridView.Columns(0).Name = "Type"
    .... ' other columns
End Sub

before entering in your main loop call this method to define name and type of your columns

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM