如何在DataGridView中显示数据,如航班信息显示

时间:2017-05-04 08:46:38

标签: vb.net

我有一个程序,它通过Timers来显示来自DataGridView中MS Access的数据 我在表单中放了2个dgvs,每个表格应显示不同表格中的不同数据 我的dgv显示14行(这是显示器的全尺寸!) 这是我试过的:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Timer1.Start()
End Sub  
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        getDATA(dgv1, dgv2, Timer1, Timer2, "tab1")
End Sub

Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
        getDATA(dgv2, dgv1, Timer2, Timer1, "tab2")
End Sub
Private Sub getDATA(dgv As DataGridView, vdgv As DataGridView, tm1 As Timer, tm2 As Timer, tbl As String)
    Try
        dgv.Visible = True
        vdgv.Visible = False
        If conn.State = ConnectionState.Open Then conn.Close()
        Dim sqlstr As String = ""
        sqlstr = "Select f1,f2,f3,f4 from " + tbl
        Dim ds As New DataTable
        Dim da As New OleDbDataAdapter(sqlstr, conn)
        ds.Reset()
        da = New OleDbDataAdapter(sqlstr, conn)
        da.Fill(ds)
        conn.Open()
        If ds.Rows.Count = 0 Then
            dgv.Rows.Clear()
        Else
            dgv.Rows.Clear()
            dgv.Rows.Add(ds.Rows.Count)
            For s = 0 To ds.Rows.Count - 1
                dgv.Rows(s).Cells(0).Value = ds.Rows(s).Item("f1")
                dgv.Rows(s).Cells(1).Value = ds.Rows(s).Item("f2")
                dgv.Rows(s).Cells(2).Value = ds.Rows(s).Item("f3")
                dgv.Rows(s).Cells(3).Value = ds.Rows(s).Item("f4")
            Next s
        End If
        dgv.ClearSelection()
        conn.Close()
        tm1.Stop()
        tm2.Start()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub  

此代码工作正常,但有时其中一个表包含超过14行! 如何显示特定DGV中的其余行?
我应该使用另一个计时器?

0 个答案:

没有答案