0

VB.NET 4.0 框架 Windows 窗体应用程序。所以我有一个 DataGridView,我在设计器中的表单上放置了一个,将所有列设置为 readOnly, AllowUserToAddRows = False, AllowUserToDeleteRows = False。现在对于代码不好的部分。

我的功能看起来像这样:

    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

我的问题是 datagridview 只显示 gridview 的 0、1、2、3 列中的值。Gridview 看起来是正确的,直到它到达事务类型所在的第 3 列。出于某种原因,应该在第 5 列中显示的数量正在该列中显示,第 4 列和第 5 列完全留空。我查看了函数的最后一个报告中的变量中包含的值 1GridView.Rows.Add 和所有变量不仅正确,而且顺序正确。所以我的问题是为什么这会失败......

4

1 回答 1

1

假设您的 DataGridView 未绑定(意味着没有自动定义列),您需要创建代码所需的适当列。然后 Row.Add(item, ....) 将起作用

例如:

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

在进入你的主循环之前调用这个方法来定义你的列的名称和类型

于 2012-04-26T21:14:55.943 回答