VB.NET 4.0框架Windows Forms Application。所以我在设计器中删除了我在表单上的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列保持完全空白。我查看了函数的最后一个reports1GridView.Rows.Add中的变量中包含的值。所有变量不仅正确,而且顺序正确。所以我的问题是为什么会失败......
答案 0 :(得分:1)
假设您的DataGridView未绑定(意味着没有自动定义列),您需要创建代码所需的相应列。然后Row.Add(item,....)将起作用
例如:
Private Sub SetupGrid()
reports1GridView.ColumnCount = 5
reports1GridView.Columns(0).Name = "Type"
.... ' other columns
End Sub
在进入主循环之前调用此方法来定义列的名称和类型