日期范围[数据网格视图的列表框]

时间:2018-05-08 15:33:49

标签: vb.net

我的代码在列表框中工作但我没有将其保存在访问数据网格视图表中。以下是我的编码。请给我一个解决方案。

background-size

My Date range project Picture

1 个答案:

答案 0 :(得分:0)

只需使用要在网格中插入的值调用Rows.Add。

Private Sub FillGridWithDates()


 'Set up the grid columns - you may already have done this at design time
        DataGridView1.ColumnCount = 2
        DataGridView1.Columns(0).Name = "si"
        DataGridView1.Columns(1).Name = "Date"

        Dim firstDate As Date
        Dim secDate As Date
        'The text property is a string, the Value property is a date
        firstDate = DateTimePicker1.Value
        secDate = DateTimePicker2.Value

        If firstDate = secDate Then
            'Do something here
        ElseIf firstDate > secDate Then
            MessageBox.Show("Please enter a valid date range")
        Else
            ListBox1.Items.Add(firstDate)
            'I just assumed a value for si
            Dim si As Integer = ListBox1.Items.Count
            DataGridView1.Rows.Add(si, firstDate)
            Do Until firstDate = secDate
                firstDate = firstDate.AddDays(1)
                ListBox1.Items.Add(firstDate)
                si = ListBox1.Items.Count
                DataGridView1.Rows.Add(si, firstDate)
            Loop
        End If
End Sub