Kimberypalet
Kimberypalet

Reputation: 119

Checking if datagridview already exist in another datagridview before inserting using checkbox

i have two datagridview and i am trying to insert value in datagridview1 to datagridview2 using checkbox on button click.

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

My code will insert the data everytime i insert it. I want to prevent inserting the same data on datagridview2. As you can see the in the image below everytime i tried to insert the same checkbox i can add it multiple times. Thank you so much.

enter image description here

This is how i populate my datagridview1.

 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

On datagridview2 i add empty column directly in datagridview2.

Thank you. Sorry for late update.

Upvotes: 0

Views: 290

Answers (2)

Mr. Tripodi
Mr. Tripodi

Reputation: 827

To me it looks like you want to move the row, if that is the case you need not worry about checking if it already exists because it would not be available to try and add it again.

        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

If it is not your intention to remove the row and you really do want to see if the row exists in datagridview2 let me know and we can make some minor modifications to this code.

Upvotes: 0

I tried my best to reproduce your environment.

        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

enter image description here

Upvotes: 0

Related Questions