如何删除DataGridView中显示的重复记录?当我尝试搜索具有关键字“dev”的记录时,DataGridView将显示其中包含“dev”的所有记录,第1行到第15行中包含“dev”,但是,在第15行之后,那里是另一组记录,与第1行到第15行完全相同。我该如何删除这些?
整个图像都是保密的,所以我把它剪裁到可能的最小尺寸。
Imports System.Data.OleDb
Imports System.IO
Imports System.Drawing.Printing
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'Database1DataSet.Tasks_Of_Offices' table. You can move, or remove it, as needed.
Me.Tasks_Of_OfficesTableAdapter.Fill(Me.Database1DataSet.Tasks_Of_Offices)
'TODO: This line of code loads data into the 'Database1DataSet.Roadmap' table. You can move, or remove it, as needed.
Me.RoadmapTableAdapter.Fill(Me.Database1DataSet.Roadmap)
'TODO: This line of code loads data into the 'Database1DataSet.Project' table. You can move, or remove it, as needed.
Me.ProjectTableAdapter.Fill(Me.Database1DataSet.Project)
'TODO: This line of code loads data into the 'Database1DataSet.OPR' table. You can move, or remove it, as needed.
Me.OPRTableAdapter.Fill(Me.Database1DataSet.OPR)
Me.WindowState = FormWindowState.Maximized
End Sub
Private Sub btnserch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnserch.Click
Search_Record()
End Sub
Private Sub Search_Record()
Dim conn As New OleDbConnection
Dim cmd As New OleDbCommand
Dim da As New OleDbDataAdapter
Dim dt As New DataTable
Dim ds As New DataSet
Dim sSQL As String = String.Empty
'try catch block is used to catch the error
Try
'get connection string declared in the Module1.vb and assign it to conn variable
conn = New OleDbConnection(Get_Constring)
conn.Open()
cmd.Connection = conn
cmd.CommandType = CommandType.Text
sSQL = "SELECT ID, Name, Manager, Office, ContactNr, Researcher, Initiatives FROM [Project]"
If Me.cbSearch.Text = "Project" Then
sSQL = sSQL & "where Name like '%" & Me.sbox.Text & "%'"
projectdg.Visible = True
oprdg.Visible = False
rmdg.Visible = False
toodg.Visible = False
Else
MsgBox("No Record Found")
End If
cmd.CommandText = sSQL
da.SelectCommand = cmd
da.Fill(dt)
sSQL = "SELECT ID, Name, Manager, Office, ContactNr, Researcher, Initiatives FROM [Project]"
If Me.cbSearch.Text = "Project" Then
sSQL = sSQL & "where Manager like '%" & Me.sbox.Text & "%'"
projectdg.Visible = True
oprdg.Visible = False
rmdg.Visible = False
toodg.Visible = False
Else
MsgBox("No Record Found")
End If
cmd.CommandText = sSQL
da.SelectCommand = cmd
da.Fill(dt)
sSQL = "SELECT ID, Name, Manager, Office, ContactNr, Researcher, Initiatives FROM [Project]"
If Me.cbSearch.Text = "Project" Then
sSQL = sSQL & "where Office like '%" & Me.sbox.Text & "%'"
projectdg.Visible = True
oprdg.Visible = False
rmdg.Visible = False
toodg.Visible = False
Else
MsgBox("No Record Found")
End If
cmd.CommandText = sSQL
da.SelectCommand = cmd
da.Fill(dt)
sSQL = "SELECT ContactNr, Researcher FROM [Project]"
If Me.cbSearch.Text = "Project" Then
sSQL = sSQL & "where ContactNr like '%" & Me.sbox.Text & "%'"
projectdg.Visible = True
oprdg.Visible = False
rmdg.Visible = False
toodg.Visible = False
Else
MsgBox("No Record Found")
End If
cmd.CommandText = sSQL
da.SelectCommand = cmd
da.Fill(dt)
sSQL = "SELECT ID, Name, Manager, Office, ContactNr, Researcher, Initiatives FROM [Project]"
If Me.cbSearch.Text = "Project" Then
sSQL = sSQL & "where Researcher like '%" & Me.sbox.Text & "%'"
projectdg.Visible = True
oprdg.Visible = False
rmdg.Visible = False
toodg.Visible = False
Else
MsgBox("No Record Found")
End If
cmd.CommandText = sSQL
da.SelectCommand = cmd
da.Fill(dt)
sSQL = "SELECT ID, Name, Manager, Office, ContactNr, Researcher, Initiatives FROM [Project]"
If Me.cbSearch.Text = "Project" Then
sSQL = sSQL & "where Initiatives like '%" & Me.sbox.Text & "%'"
projectdg.Visible = True
oprdg.Visible = False
rmdg.Visible = False
toodg.Visible = False
Else
MsgBox("No Record Found")
End If
cmd.CommandText = sSQL
da.SelectCommand = cmd
da.Fill(dt)
Me.projectdg.DataSource = dt
If dt.Rows.Count = 0 Then
MsgBox("No Record Found")
End If
Catch ex As Exception
MsgBox(ErrorToString)
Finally
conn.Close()
End Try
End Sub
答案 0 :(得分:0)
所以,等待答案让我的头发变白了。我已经找到了我的问题的答案。所以我在这部分添加了一个代码
sSQL = "SELECT ID, Name, Manager, Office, ContactNr, Researcher, Initiatives FROM [Project]"
If Me.cbSearch.Text = "Project" Then
sSQL = sSQL & "where Name like '%" & Me.sbox.Text & "%'"
projectdg.Visible = True
oprdg.Visible = False
rmdg.Visible = False
toodg.Visible = False
Else
MsgBox("No Record Found")
End If
cmd.CommandText = sSQL
da.SelectCommand = cmd
da.Fill(dt)
是RemoveDuplicateRows(dt,“nameofcolumn”) 并添加了另一个代码,
Public Function RemoveDuplicateRows(ByVal dTable As DataTable, ByVal colName As String) As DataTable
Dim hTable As New Hashtable()
Dim duplicateList As New ArrayList()
For Each dtRow As DataRow In dTable.Rows
If hTable.Contains(dtRow(colName)) Then
duplicateList.Add(dtRow)
Else
hTable.Add(dtRow(colName), String.Empty)
End If
Next
For Each dtRow As DataRow In duplicateList
dTable.Rows.Remove(dtRow)
Next
Return dTable
End Function