【问题标题】:Adding rows to a dataGridView dynamically动态地将行添加到 dataGridView
【发布时间】:2012-04-26 19:53:15
【问题描述】:

VB.NET 4.0 框架 Windows 窗体应用程序。所以我有一个 DataGridView,我在设计器的表单上放置了一个,将所有列设置为只读,AllowUserToAddRows = FalseAllowUserToDeleteRows = 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 和所有变量不仅正确,而且顺序正确。所以我的问题是为什么会失败......

【问题讨论】:

  • 应该注意的是,我尝试像这样手动为所有列分配值:reports1GridView.Rows.Add("Type","Name","Number","Transaction Type",交易日期”,“交易金额”),它以与上述相同的模式失败......

标签: vb.net winforms datagridview


【解决方案1】:

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

例如:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多