在条件中显示DatagridView中的数据

时间:2018-05-06 17:01:44

标签: vb.net

我的数据库名为DataOberge,表名为TableOberge和字段(Id,FirstName,Phone,DateOut,HourOut,DateIN,HourIN).field DateOut和DateIN的类型为Date.field HourOut,HourIN的类型为Time。 如何在datagridview2中显示今天到达的人的日期和时间取决于字段DateIN和HourIN。 这是我的全部代码:

 Imports System.Data.SqlClient
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

Dim InfoCommand As SqlCommand         Dim StrCon As SqlConnection = Nothing         尝试             StrCon = New SqlConnection(“data source = ABIDINE; initial catalog = DataOberge; User ID = sa; Password = 123456789;”)             使用DepCom作为SqlCommand = New SqlCommand(“Select * From TableOberge”,StrCon)                 StrCon.Open()                 使用DepAdap作为SqlDataAdapter =新的SqlDataAdapter(DepCom)                     Dim DepDT As DataTable = New DataTable                     DepAdap.Fill(DepDT)                     DataGridView1.DataSource = DepDT                     Dim CurrentBs As BindingSource = New BindingSource()                     CurrentBs.DataSource = DepDT                     DataGridView2.DataSource = CurrentBs                     'CurrentBs.Filter = String.Format(“[DateIN] =#{0}#AND [HourIN]> =#{1}#”,DateTime.Now.Date,DateTime.Now.Hour)                     CurrentBs.Filter = String.Format(“[DateIN] =#{0}#AND [HourIN]> =#{1}#”,DateTime.Now.Date,DateTime.Now.Hour)                 结束使用                 StrCon.Close()             结束使用         赶上前例外             Console.WriteLine(ex.Message)         最后             如果StrCon IsNot Nothing那么                 如果StrCon.State = ConnectionState.Open那么                     StrCon.Close()                 万一                 StrCon.Dispose()             万一         结束尝试     结束子

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim StrCon As New SqlConnection
    StrCon = New SqlConnection("data source=ABIDINE; initial catalog= DataOberge;User ID=sa;Password=123456789;")
    Using Command As New SqlCommand With {.Connection = StrCon}
        With Command
            .CommandText = "INSERT INTO [TableOberge] ([ID], [FIRSTNAME], [PHONE], [ADRESSE], [DATE_OUT], [HOUR_OUT], [DATE_IN], [HOUR_IN]) VALUES (@ID, @FIRSTNAME, @PHONE, @ADRESSE, @DATE_OUT, @HOUR_OUT, @DATE_IN, @HOUR_IN)"
            .Parameters.Add("@ID", SqlDbType.Int).Value = TextBox1.Text
            .Parameters.Add("@FIRSTNAME", SqlDbType.NVarChar).Value = TextBox2.Text
            .Parameters.Add("@PHONE", SqlDbType.NVarChar).Value = TextBox3.Text
            .Parameters.Add("@ADRESSE", SqlDbType.NVarChar).Value = TextBox4.Text
            .Parameters.Add("@DATE_OUT", SqlDbType.Date).Value = TextBox5.Text
            .Parameters.Add("@HOUR_OUT", SqlDbType.Time).Value = TextBox6.Text
            .Parameters.Add("@DATE_IN", SqlDbType.Date).Value = TextBox7.Text
            .Parameters.Add("@HOUR_IN", SqlDbType.Time).Value = TextBox8.Text
        End With
        If StrCon.State = ConnectionState.Closed Then StrCon.Open()
        If Command.ExecuteNonQuery() = 1 Then
            MsgBox("SUCCED ADD", MsgBoxStyle.MsgBoxRtlReading, "SUCCES")
        Else
            MsgBox("ERROR FATAL", MsgBoxStyle.MsgBoxRtlReading, "ERROR")
        End If
        StrCon.Close()
    End Using
End Sub

结束班

1 个答案:

答案 0 :(得分:0)

您可以在SQL命令中指定填充DataGridView2中的数据的WHERE子句。最好将BindingSource添加到Form并将其绑定到您填充的DataTable,这样您就可以设置过滤器了。尝试这样的事情:

'Declare the connection object
Dim StrCon As SqlConnection = Nothing

'Wrap code in Try/Catch
Try
    'Set the connection object to a new instance
    'TODO: Change "My Connection String Here" with a valid connection string
    StrCon = New SqlConnection("My Connection String Here")

    'Create a new instance of the command object
    Using DepCom As SqlCommand = New SqlCommand("Select * From TableOberge", StrCon)

        'Open the connection
        StrCon.Open()

        'Create a new instance of the data adapter object
        Using DepAdap As SqlDataAdapter = New SqlDataAdapter(DepCom)
            'Create a new instance of a DataTable
            Dim DepDT As DataTable = New DataTable

            'Use the DataAdapter to fill the data from the SqlCommand into the DataTable
            DepAdap.Fill(DepDT)

            'Set the DataSource of the DataGridView to the filled DataTable
            DataGridView1.DataSource = DepDT

            'Create a new instance of a BindingSource
            Dim CurrentBs As BindingSource = New BindingSource()

            'Setup the properties of the BindingSource
            CurrentBs.DataSource = DepDT

            'Bind the BindingSource to the DataGridView
            DataGridView2.DataSource = CurrentBs

            'Filter the data in the BindingSource to today's date and anything on or after the current hour
            CurrentBs.Filter = $"[DateIN] = #{DateTime.Now.Date}# AND [HourIN] >= #{DateTime.Now.Hour}#"
        End Using

        'Close the connection
        StrCon.Close()
        End Using
Catch ex As Exception
    'Display the error
    Console.WriteLine(ex.Message)
Finally
    'Check if the connection object was initialized
    If StrCon IsNot Nothing Then
        If StrCon.State = ConnectionState.Open Then
            'Close the connection if it was left open(exception thrown)
            StrCon.Close()
        End If

        'Dispose of the connection object
        StrCon.Dispose()
    End If
End Try

或者,如果您不想使用BindingSource,那么只需创建一个新的SqlCommand和SqlDataAdapter,然后使用适当的SQL查询仅为您的DataGridView2执行相同的操作。

<强>更新

在我的示例中,我使用String Interpolation,但我发现OP使用的是不支持它的Visual Studios版本。因此,请更改以下内容:

'From:
CurrentBs.Filter = $"[DateIN] = #{DateTime.Now.Date}# AND [HourIN] >= #{DateTime.Now.Hour}#"

'To:
CurrentBs.Filter = String.Format("[DateIN] = #{0}# AND [HourIN] >= #{1}#", DateTime.Now.Date, DateTime.Now.Hour)