2015-08-28 62 views
1

我想創建一個datagridview每一天。這是我的代碼到目前爲止。它不顯示任何錯誤,但是當我運行代碼時,它只是加載表單,但沒有出現dgv。我能做什麼?循環槽日期vb.net

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load 
     Dim start As Date = Date.Now 
     Dim apocap As Date = Date.Parse(#8/22/2050#) 
     Dim loopdate As Date = start 

     While start < apocap 
      Dim dgv As New DataGridView 
      With dgv 
       .Size = New Size(250, 250) 
       .ColumnCount = 2 
       .RowCount = 12 
       .Location = New Point(12, 9) 
       .Visible = True 

      End With 

      start = start.Date.AddDays(1) 
     End While 
    End Sub 
End Class 

回答

1

您必須將它們添加到您的表單中。此外,你要改變.Left和/或.Top.Location)屬性,使他們不彼此頂部所有的堆棧:

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load 
    Dim start As DateTime = DateTime.Today 
    Dim apocap As New DateTime(2050, 8, 22) 
    Dim i As Integer = 0 

    While start < apocap 
     Dim dgv As New DataGridView 
     With dgv 
      .Size = New Size(250, 250) 
      .ColumnCount = 2 
      .RowCount = 12 
      .Location = New Point(12, (9 + (250 * i))) 
      .Visible = True 
     End With 

     Me.Controls.Add(dgv) 
     i += 1 
     start = start.AddDays(1) 
    End While 
End Sub 

爲了好玩,我喜歡它這樣寫:

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load 
    Dim start As DateTime = DateTime.Today 
    Dim apocap As New DateTime(2050, 8, 22) 
    Dim count As Integer = CInt((apocap - start).TotalDays) 

    Me.Controls.AddRange(Enumerable.Range(0, count).Select(
     Function(i) 
      Return New DataGridView With { 
      .Size = New Size(250, 250), 
      .ColumnCount = 21, 
      .RowCount = 12, 
      .Location = New Point(12, (9 + (250 * i))), 
      .Visible = True 
      } 
      'start.AddDays(i) is there if you need it 
     End Function 
    ).ToArray()) 
End Sub