2012-04-26 99 views
0

VB.NET 4.0框架Windows窗体应用程序。所以我有一个DataGridView,我放在设计器中的表单上,将所有列设置为只读,AllowUserToAddRows = False,AllowUserToDeleteRows = False。现在是代码不好的地方。动态添加行到dataGridView

我的函数如下所示:

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和所有的变量不仅是正确的,而且是正确的顺序。所以我的问题是为什么这个失败...

+0

它应该注意,我试图手动为所有coloumns赋值如下:reports1GridView.Rows.Add(“Type”,“Name”,“Number”,“Transaction Type”,Transaction Date“,”Transaction Amount“)并且与上述相同的模式失败了...... – Skindeep2366 2012-04-26 21:09:45

回答

1

假设你的DataGridView没有绑定(意味着没有自动定义的列),你需要创建你的代码所需的适当的列。然后Row.Add(项目,....)将工作

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

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