避免 datagridview 中的重复数据 vb.net
Avoid duplicate data in datagridview vb.net
早上好,我有以下问题,如何验证记录在我的数据网格视图中没有重复。
这是我将数据从 gridview 列表发送到 gridview 详细信息的代码
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim rowSelected As List(Of DataGridViewRow) = New List(Of DataGridViewRow)()
For Each row As DataGridViewRow In dgvlistarproductos.Rows
Dim cellSelecion As DataGridViewCheckBoxCell = TryCast(row.Cells("SELECCIONAR"), DataGridViewCheckBoxCell)
If Convert.ToBoolean(cellSelecion.Value) Then
rowSelected.Add(row)
End If
Next
For Each row As DataGridViewRow In rowSelected
dgvdetalleproduc.Rows.Add(New Object() {row.Cells(1).Value, row.Cells(2).Value
})
Next
End Sub
任何建议或帮助我知道我必须验证但我不知道放在哪里,谢谢
尝试将验证代码放在 DataGridView 控件的 RowValidating 事件中,或尝试下面的代码:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim rowSelected As List(Of DataGridViewRow) = New List(Of DataGridViewRow)()
For Each row As DataGridViewRow In dgvlistarproductos.Rows
Dim cellSelecion As DataGridViewCheckBoxCell = TryCast(row.Cells("SELECCIONAR"), DataGridViewCheckBoxCell)
If Convert.ToBoolean(cellSelecion.Value) Then
rowSelected.Add(row)
End If
Next
For Each row As DataGridViewRow In rowSelected
If dgvdetalleproduc.Rows.Contains(row) = False Then
dgvdetalleproduc.Rows.Add(New Object() {row.Cells(1).Value, row.Cells(2).Value})
End If
Next
End Sub
如果 IDPRODUCTO
听起来像 唯一产品 ID ,那么使用它来识别并通过 LINQ 查询跳过重复项,如下所示:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim checkedRows = dgvlistarproductos.Rows.Cast(Of DataGridViewRow).
Where(Function(r1) r1.Cells(0).Value IsNot Nothing AndAlso Convert.ToBoolean(r1.Cells(0).Value)).
Where(Function(r1) Not dgvdetalleproduc.Rows.Cast(Of DataGridViewRow).
Any(Function(r2) r2.Cells(0).Value IsNot Nothing AndAlso r2.Cells(0).Value.ToString().
Equals(r1.Cells(1).ToString(), StringComparison.OrdinalIgnoreCase)))
For Each row In checkedRows
dgvdetalleproduc.Rows.Add(row.Cells(1).Value, row.Cells(2).Value)
Next
End Sub
您可以使用列名而不是它们的索引来获取单元格值。
最简单的方法是让两个网格都使用一个数据源(数据表)来获取它们的信息,并在数据表中有一个布尔列,如“isChosen”。每个网格绑定到一个单独的绑定源或数据视图,其中一个将其 [Row]Filter 设置为 "[isChosen] = True"
,其他过滤器设置为 false。 >>
按钮将选中的行设置为 true,另一个按钮将其设置为 false
因为只有一行,而且它不会“移动”到任何地方(它只是根据 isChosen 的值出现在不同的网格中)所以没有重复项
为了证明这一点:
- 制作新表格
- 在上面放两个datagridview
- 在上面放两个按钮
- 将此代码放在 InitializeComponent 之后的构造函数中:
Dim dt as New DataTable
dt.Columns.Add("Name")
dt.Columns.Add("IsChosen", GetType(Boolean))
dt.Rows.Add("John", False)
dt.Rows.Add("Mark", False)
dt.Rows.Add("Luke", False)
Dim bs1 = New BindingSource()
bs1.Filter = "[IsChosen] = False"
bs1.DataSource = dt
dataGridView1.DataSource = bs1
Dim bs2 = New BindingSource()
bs2.Filter = "[IsChosen] = True"
bs2.DataSource = dt
dataGridView2.DataSource = bs2
- 双击按钮 1 并输入代码
DirectCast(DirectCast(dataGridView1.DataSource, BindingSource).Current, DataRowView)("IsChosen") = True
- 双击按钮 2 并输入代码
DirectCast(DirectCast(dataGridView2.DataSource, BindingSource).Current, DataRowView)("IsChosen") = False
运行 应用程序,点击dgv1中的一个名称,然后点击button1,它会移动到dgv2
您还可以切换网格中 ✅ 复选框列的状态,它会移动 - 数据表列如何变为 True/False 将行移动到 dgv 2/1 并不重要分别
感谢两位给我解决这个案子的思路。
我设法解决并检测了在使用以下代码传递给 Datagridview 时所选记录是否重复或重复
Dim existe = False
Dim rowSelected As List(Of DataGridViewRow) = New List(Of DataGridViewRow)()
For Each row As DataGridViewRow In dgvlistarproductos.Rows
Dim cellSelecion As DataGridViewCheckBoxCell = TryCast(row.Cells("SELECCIONAR"), DataGridViewCheckBoxCell)
If Convert.ToBoolean(cellSelecion.Value) Then
rowSelected.Add(row)
End If
Next
For Each row As DataGridViewRow In dgvdetalleproduc.Rows
If Convert.ToString(row.Cells(0).Value).Equals(dgvlistarproductos.CurrentRow.Cells(1).Value) Then
existe = True
End If
Next
For Each row As DataGridViewRow In rowSelected
If (existe = True) Then
MsgBox("ESTE PRODUCTO YA FUE AGREGADO", MsgBoxStyle.OkOnly, "INFORMACIÓN")
Else
dgvdetalleproduc.Rows.Add(New Object() {row.Cells(1).Value, row.Cells(2).Value
})
End If
Next
早上好,我有以下问题,如何验证记录在我的数据网格视图中没有重复。
这是我将数据从 gridview 列表发送到 gridview 详细信息的代码
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim rowSelected As List(Of DataGridViewRow) = New List(Of DataGridViewRow)()
For Each row As DataGridViewRow In dgvlistarproductos.Rows
Dim cellSelecion As DataGridViewCheckBoxCell = TryCast(row.Cells("SELECCIONAR"), DataGridViewCheckBoxCell)
If Convert.ToBoolean(cellSelecion.Value) Then
rowSelected.Add(row)
End If
Next
For Each row As DataGridViewRow In rowSelected
dgvdetalleproduc.Rows.Add(New Object() {row.Cells(1).Value, row.Cells(2).Value
})
Next
End Sub
任何建议或帮助我知道我必须验证但我不知道放在哪里,谢谢
尝试将验证代码放在 DataGridView 控件的 RowValidating 事件中,或尝试下面的代码:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim rowSelected As List(Of DataGridViewRow) = New List(Of DataGridViewRow)()
For Each row As DataGridViewRow In dgvlistarproductos.Rows
Dim cellSelecion As DataGridViewCheckBoxCell = TryCast(row.Cells("SELECCIONAR"), DataGridViewCheckBoxCell)
If Convert.ToBoolean(cellSelecion.Value) Then
rowSelected.Add(row)
End If
Next
For Each row As DataGridViewRow In rowSelected
If dgvdetalleproduc.Rows.Contains(row) = False Then
dgvdetalleproduc.Rows.Add(New Object() {row.Cells(1).Value, row.Cells(2).Value})
End If
Next
End Sub
如果 IDPRODUCTO
听起来像 唯一产品 ID ,那么使用它来识别并通过 LINQ 查询跳过重复项,如下所示:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim checkedRows = dgvlistarproductos.Rows.Cast(Of DataGridViewRow).
Where(Function(r1) r1.Cells(0).Value IsNot Nothing AndAlso Convert.ToBoolean(r1.Cells(0).Value)).
Where(Function(r1) Not dgvdetalleproduc.Rows.Cast(Of DataGridViewRow).
Any(Function(r2) r2.Cells(0).Value IsNot Nothing AndAlso r2.Cells(0).Value.ToString().
Equals(r1.Cells(1).ToString(), StringComparison.OrdinalIgnoreCase)))
For Each row In checkedRows
dgvdetalleproduc.Rows.Add(row.Cells(1).Value, row.Cells(2).Value)
Next
End Sub
您可以使用列名而不是它们的索引来获取单元格值。
最简单的方法是让两个网格都使用一个数据源(数据表)来获取它们的信息,并在数据表中有一个布尔列,如“isChosen”。每个网格绑定到一个单独的绑定源或数据视图,其中一个将其 [Row]Filter 设置为 "[isChosen] = True"
,其他过滤器设置为 false。 >>
按钮将选中的行设置为 true,另一个按钮将其设置为 false
因为只有一行,而且它不会“移动”到任何地方(它只是根据 isChosen 的值出现在不同的网格中)所以没有重复项
为了证明这一点:
- 制作新表格
- 在上面放两个datagridview
- 在上面放两个按钮
- 将此代码放在 InitializeComponent 之后的构造函数中:
Dim dt as New DataTable
dt.Columns.Add("Name")
dt.Columns.Add("IsChosen", GetType(Boolean))
dt.Rows.Add("John", False)
dt.Rows.Add("Mark", False)
dt.Rows.Add("Luke", False)
Dim bs1 = New BindingSource()
bs1.Filter = "[IsChosen] = False"
bs1.DataSource = dt
dataGridView1.DataSource = bs1
Dim bs2 = New BindingSource()
bs2.Filter = "[IsChosen] = True"
bs2.DataSource = dt
dataGridView2.DataSource = bs2
- 双击按钮 1 并输入代码
DirectCast(DirectCast(dataGridView1.DataSource, BindingSource).Current, DataRowView)("IsChosen") = True
- 双击按钮 2 并输入代码
DirectCast(DirectCast(dataGridView2.DataSource, BindingSource).Current, DataRowView)("IsChosen") = False
运行 应用程序,点击dgv1中的一个名称,然后点击button1,它会移动到dgv2
您还可以切换网格中 ✅ 复选框列的状态,它会移动 - 数据表列如何变为 True/False 将行移动到 dgv 2/1 并不重要分别
感谢两位给我解决这个案子的思路。
我设法解决并检测了在使用以下代码传递给 Datagridview 时所选记录是否重复或重复
Dim existe = False
Dim rowSelected As List(Of DataGridViewRow) = New List(Of DataGridViewRow)()
For Each row As DataGridViewRow In dgvlistarproductos.Rows
Dim cellSelecion As DataGridViewCheckBoxCell = TryCast(row.Cells("SELECCIONAR"), DataGridViewCheckBoxCell)
If Convert.ToBoolean(cellSelecion.Value) Then
rowSelected.Add(row)
End If
Next
For Each row As DataGridViewRow In dgvdetalleproduc.Rows
If Convert.ToString(row.Cells(0).Value).Equals(dgvlistarproductos.CurrentRow.Cells(1).Value) Then
existe = True
End If
Next
For Each row As DataGridViewRow In rowSelected
If (existe = True) Then
MsgBox("ESTE PRODUCTO YA FUE AGREGADO", MsgBoxStyle.OkOnly, "INFORMACIÓN")
Else
dgvdetalleproduc.Rows.Add(New Object() {row.Cells(1).Value, row.Cells(2).Value
})
End If
Next