移除 datagridview 中的重复数据,使用 VB.NET。

huangapple go评论128阅读模式
英文:

Remove The duplicate data in datagridview vb.net

问题

以下是您提供的代码部分的翻译:

所以我想打印一个月中的日期从1到31并且在MySQL中具有相应的时间进和时间出值但它在数据网格视图中重复了这个值

如何消除这个重复值

子加载表()
        打开连接()
        ' 登录.txt_identifier.text '
        尝试
            Dim cm As Date = Now
            Dim Month As String = "MMMM"
            Dim query As String = "SELECT Day , Time_in ,Time_out FROM sample_work_hours WHERE student_number = '" & login.txt_identifier.Text & "' AND month = '" & cm.ToString(Month) & "' "
            insert_data.Connection = connect_data
            insert_data.CommandText = query
            适配器.SelectCommand = insert_data
            表.Clear()
            适配器.Fill(表)
            DataGridView1.DataSource =            DataGridView1.Columns(0).HeaderText = "Day"
            DataGridView1.Columns(1).HeaderText = "Time In"
            DataGridView1.Columns(2).HeaderText = "Time Out"
            对于行作为整数 = 1 到 31
                Dim newrow As DataRow = 表.NewRow
                newrow(0) =                Dim dayDataRow As DataRow() = 表.Select("Day = '" && "'")
                如果 dayDataRow.Length > 0 则
                    newrow(1) = dayDataRow(0)("Time_in") ' 设置Time_in值
                    newrow(2) = dayDataRow(0)("Time_out") ' 设置Time_out值
                否则
                    newrow(1) = "" ' 如果找不到相应的日期数据,则设置为空字符串
                    newrow(2) = "" ' 如果找不到相应的日期数据,则设置为空字符串
                结束 如果
                表.Rows.Add(newrow)
            下一个
            connect_data.Close()
        捕获异常 ex 作为异常
            MsgBox(ex.ToString)
        结束 尝试
    结束 子

请注意,这是代码部分的中文翻译,没有包含问题的回答或其他内容。

英文:

so, I want to print days in a month which is 1 to 31 and with the corresponding time in and time out value in MySQL, but it duplicates the value in data grid view.

how to can get rid of this duplicate value?

Sub loadtable()
        open_connection()
        '" & login.txt_identifier.text & "'


        Try
            Dim cm As Date = Now
            Dim Month As String = "MMMM"
            Dim query As String = "SELECT Day , Time_in ,Time_out FROM sample_work_hours WHERE student_number = '" & login.txt_identifier.Text & "' AND month = '" & cm.ToString(Month) & "' "
            insert_data.Connection = connect_data
            insert_data.CommandText = query



            adapter.SelectCommand = insert_data
            table.Clear()
            adapter.Fill(table)
            DataGridView1.DataSource = table

            DataGridView1.Columns(0).HeaderText = "Day"
            DataGridView1.Columns(1).HeaderText = "Time In"
            DataGridView1.Columns(2).HeaderText = "Time Out"

            For row As Integer = 1 To 31

                Dim newrow As DataRow = table.NewRow
                newrow(0) = row

                Dim dayDataRow As DataRow() = table.Select("Day = '" & row & "'")
                If dayDataRow.Length > 0 Then
                    newrow(1) = dayDataRow(0)("Time_in") ' Set the Time_in value
                    newrow(2) = dayDataRow(0)("Time_out") ' Set the Time_out value
                Else
                    newrow(1) = "" ' Set empty string if no corresponding day data found
                    newrow(2) = "" ' Set empty string if no corresponding day data found
                End If

                table.Rows.Add(newrow)


            Next


            connect_data.Close()

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

    End Sub

移除 datagridview 中的重复数据,使用 VB.NET。

答案1

得分: 1

"SELECT DISTINCT Day, Time_in, Time_out FROM sample_work_hours WHERE student_number = '" + login.txt_identifier.Text + "' AND month = '" + cm.ToString(Month) + "'"

英文:

Dim query As String = "SELECT DISTINCT Day , Time_in ,Time_out FROM sample_work_hours WHERE student_number = '" & login.txt_identifier.Text & "' AND month = '" & cm.ToString(Month) & "' "

答案2

得分: 0

I just solve it by customize the columns and add the specific data for it by checking the day.

对此问题,我通过自定义列并通过检查日期来添加特定数据来解决它。

英文:

移除 datagridview 中的重复数据,使用 VB.NET。

i just solve it by customize the columns and add the specific data for it by checking the day

For row As Integer = 1 To 31

                 Dim newrow As New DataGridViewRow()
                newrow.CreateCells(DataGridView1)
                newrow.Cells(0).Value = row

                Dim dayDataRow As DataRow() = table.Select("Day = '" & row & "'")

                If dayDataRow.Length > 0 Then
                    DataGridView1.Rows.Add(row, dayDataRow(0)("Time_in"), dayDataRow(0)("Time_out"))
                Else
                    DataGridView1.Rows.Add(row, "", "")
                End If


            Next

            connect_data.Close()

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

i remove this code

        DataGridView1.DataSource = table

        DataGridView1.Columns(0).HeaderText = "Day"
        DataGridView1.Columns(1).HeaderText = "Time In"
        DataGridView1.Columns(2).HeaderText = "Time Out"

If dayDataRow.Length > 0 Then
                newrow(1) = dayDataRow(0)("Time_in") ' Set the Time_in value
                newrow(2) = dayDataRow(0)("Time_out") ' Set the Time_out value
            Else
                newrow(1) = "" ' Set empty string if no corresponding day data found
                newrow(2) = "" ' Set empty string if no corresponding day data found
            End If

and add this since i want to customize the data in grid view

            Dim newrow As New DataGridViewRow()
            newrow.CreateCells(DataGridView1)
            newrow.Cells(0).Value = row

huangapple
  • 本文由 发表于 2023年4月11日 15:56:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75983620.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定