2012-04-26 168 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