首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在使用复选框插入之前,检查datagridview是否已存在于另一个datagridview中

在使用复选框插入之前,检查datagridview是否已存在于另一个datagridview中
EN

Stack Overflow用户
提问于 2019-05-21 21:25:34
回答 2查看 85关注 0票数 0

我有两个datagridview,我正在尝试使用按钮单击时的复选框将datagridview1中的值插入到datagridview2中。

代码语言:javascript
复制
For i As Integer = DataGridView1.Rows.Count - 1 To 0 Step -1

        Dim c As Boolean
        c = DataGridView1.Rows(i).Cells(0).Value

        If c = True Then
            With DataGridView1.Rows(i)
                DataGridView2.Rows.Insert(0, .Cells(0).Value, .Cells(1).Value, .Cells(2).Value, .Cells(3).Value, .Cells(4).Value, .Cells(5).Value)
            End With
        End If

    Next

每次插入数据时,我的代码都会插入数据。我想防止在datagridview2上插入相同的数据。正如您在下图中看到的,每次我尝试插入相同的复选框时,我都可以多次添加它。非常感谢。

这就是我填充datagridview1的方式。

代码语言:javascript
复制
 Sub dgv1_SubjectList()
    query = "SELECT subject_id AS '#', subject_name AS 'Descriptive Title', subject_units AS 'Units', sem AS 'Semester', year_level AS 'Year Level' " & _
                " FROM subject WHERE course = '" & cmb_Course.Text & "' AND CURRICULUM = '" & curriculum & "' "
    da = New MySqlDataAdapter(query, myconn)
    da.Fill(ds, "subject")
    DataGridView1.DataSource = ds.Tables(0)
    DataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)
End Sub

在datagridview2中,我直接在datagridview2中添加空列。

谢谢。很抱歉更新晚了。

EN

回答 2

Stack Overflow用户

发布于 2019-05-21 22:34:02

我尽了最大努力重现你的环境。

代码语言:javascript
复制
        DataGridView1.AllowUserToAddRows = True
    DataGridView2.AllowUserToAddRows = True
    DataGridView1.Rows.Insert(DataGridView1.Rows.Count - 1, True, "Row1Col1", "Row1Col2", "Row1Col3", "Row1Col4", "Row1Col5", "Row1Col6")
    DataGridView1.Rows.Insert(DataGridView1.Rows.Count - 1, True, "Row2Col1", "Row2Col2", "Row2Col3", "Row2Col4", "Row2Col5", "Row2Col6")

    DataGridView2.Rows.Insert(DataGridView2.Rows.Count - 1, False, "Row1Col1", "Row1Col2", "Row1Col3", "Row1Col4", "Row1Col5", "Row1Col6")

    For i As Integer = DataGridView1.Rows.Count - 2 To 0 Step -1

        If DataGridView1.Rows(i).Cells(0).Value Then
            Dim AlreadyInGrid As Boolean = False
            Dim ColumnsCount As Integer = DataGridView1.Columns.GetColumnCount(DataGridViewElementStates.Displayed)
            For j As Integer = DataGridView1.Rows.Count - 2 To 0 Step -1
                For k As Integer = 1 To ColumnsCount - 1
                    If DataGridView1.Rows(i).Cells(k).Value <> DataGridView2.Rows(j).Cells(k).Value Then Exit For
                    AlreadyInGrid = (k = ColumnsCount - 1)
                Next
                If AlreadyInGrid Then Exit For
            Next

            If Not AlreadyInGrid Then
                With DataGridView1.Rows(i)
                    DataGridView2.Rows.Insert(DataGridView2.Rows.Count - 1, False, .Cells(1).Value, .Cells(2).Value, .Cells(3).Value, .Cells(4).Value, .Cells(5).Value, .Cells(6).Value)
                End With
            End If

        End If

    Next

票数 0
EN

Stack Overflow用户

发布于 2019-05-22 03:13:46

在我看来,您似乎想要移动行,如果是这样的话,您不必担心检查它是否已经存在,因为它将无法再次尝试添加它。

代码语言:javascript
复制
        Public Class Form1
            Dim ds As New DataSet
            Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
                'Begin sample setup


                ds.Tables.Add(New DataTable With {.TableName = "Table1"})
                With ds.Tables(0)
                    .Columns.Add("Select", GetType(Boolean))
                    .Columns.Add("ID", GetType(Integer))
                    .Columns.Add("Column2", GetType(String))
                    .Rows.Add(False, 1, "test1")
                    .Rows.Add(False, 2, "test2")
                    .Rows.Add(False, 3, "test3")
                    .Rows.Add(False, 4, "test4")
                End With

                Dim dt As DataTable = ds.Tables(0).Clone
                dt.TableName = "Table2"

                ds.Tables.Add(dt)
                DataGridView1.DataSource = ds.Tables(0)
                DataGridView2.DataSource = ds.Tables(1)
                'end sample setup
            End Sub

            Private Sub ButtonAddToDT2_Click(sender As Object, e As EventArgs) Handles ButtonAddToDT2.Click
                Validate()
                Dim SelRows() As DataRow = ds.Tables(0).Select("Select=True")
                For Each DtRow As DataRow In SelRows
                    ds.Tables(1).ImportRow(DtRow)
                    ds.Tables(0).Rows.Remove(DtRow)
                Next
            End Sub

        End Class

如果您不打算删除该行,并且确实希望查看行是否存在于datagridview2中,请让我知道,我们可以对此代码进行一些细微的修改。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56239337

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档