英文:
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
答案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.
对此问题,我通过自定义列并通过检查日期来添加特定数据来解决它。
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论