none
Using Multiple DataGridView RRS feed

  • Question

  • Hi Everyone

    i'm a beginner in programming language,
    could someone help me.

    i have 3 datagridviews in my form.
    datagridview1 from my database (excel)
    datagridview2 from textbox.text
    datagridview3 from my database and textbox.text (with removed duplicate)

    my question is, how do i input datagridview 3 from my database and textbox.text with removed duplicate item both of datagridview1 and datagridview2.

    thank you very much

    Sunday, September 3, 2017 6:01 AM

Answers

  • Hi Mike,

    I assume that you have fill data into datagridview1 and datagridview2, now you want to transfer data from datagridview1 and datagridview2 into datagridview3, then remove duplicate the same item. I do one sample that you can refer to:

     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim dt As New DataTable
            dt.Columns.Add("Column1", GetType(Int32))
            dt.Columns.Add("Column2", GetType(String))
            dt.Columns.Add("Column3", GetType(String))
    
            dt.Rows.Add(1, "AA", "aa")
            dt.Rows.Add(2, "BB", "bb")
            dt.Rows.Add(3, "CC", "cc")
            dt.Rows.Add(4, "DD", "dd")
            dt.Rows.Add(5, "EE", "ee")
    
            DataGridView1.DataSource = dt
    
            Dim dt1 As New DataTable
            dt1.Columns.Add("Column1", GetType(Int32))
            dt1.Columns.Add("Column2", GetType(String))
            dt1.Columns.Add("Column3", GetType(String))
    
            dt1.Rows.Add(6, "HH", "hh")
            dt1.Rows.Add(7, "FF", "ff")
            dt1.Rows.Add(3, "CC", "cc")
            dt1.Rows.Add(4, "GG", "gg")
            dt1.Rows.Add(9, "EE", "ee")
    
            DataGridView2.DataSource = dt1
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            For i As Integer = 0 To DataGridView1.Rows.Count - 1
                DataGridView3.Rows.Add()
    
                For j As Integer = 0 To DataGridView1.Rows(i).Cells.Count - 1
                    DataGridView3.Rows(i).Cells(j).Value = DataGridView1.Rows(i).Cells(j).Value
                Next
            Next
            Dim rowindex As Integer = DataGridView1.Rows.Count - 1
            For i As Integer = 0 To DataGridView2.Rows.Count - 1
                DataGridView3.Rows.Add()
                rowindex = rowindex + 1
    
                For j As Integer = 0 To DataGridView2.Rows(i).Cells.Count - 1
                    DataGridView3.Rows(rowindex).Cells(j).Value = DataGridView2.Rows(i).Cells(j).Value
                Next
            Next
    
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            For i As Integer = 0 To Me.DataGridView3.RowCount - 2
                For j As Integer = i + 1 To Me.DataGridView3.RowCount - 2
                    If DataGridView3.Rows(i).Cells(0).Value = DataGridView3.Rows(j).Cells(0).Value Then
    
                        DataGridView3.Rows.Remove(DataGridView3.Rows(i))
                        i -= 1
                        Debug.Print("duplicate value " & DataGridView3.Rows(i).Cells(0).Value)
                        'DataGridView1.Rows(i).Cells(1).Value = "Duplicate"
                    End If
                Next
            Next
        End Sub

    Copy the data from datagridview1 and datagridview2 into datagridview3 in Button1_click event, remove the duplicate item from datagridview3 in Button2_click event.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Mike Zed Monday, September 4, 2017 6:48 PM
    Monday, September 4, 2017 9:13 AM
    Moderator

All replies

  • my question is, how do i input datagridview 3 from my database and textbox.text with removed duplicate item both of datagridview1 and datagridview2.

    That would depend on how you are creating the contents for DataGridView 1 and DataGridView 2.  For instance, if you are using tables as the data source for DGV 1 and DGV 2, then you would create another table that is the union of those two tables, and populate DGV 3 in the same way. A union is distinct by default.

    Sunday, September 3, 2017 6:56 AM
  • Hi Mike,

    I assume that you have fill data into datagridview1 and datagridview2, now you want to transfer data from datagridview1 and datagridview2 into datagridview3, then remove duplicate the same item. I do one sample that you can refer to:

     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim dt As New DataTable
            dt.Columns.Add("Column1", GetType(Int32))
            dt.Columns.Add("Column2", GetType(String))
            dt.Columns.Add("Column3", GetType(String))
    
            dt.Rows.Add(1, "AA", "aa")
            dt.Rows.Add(2, "BB", "bb")
            dt.Rows.Add(3, "CC", "cc")
            dt.Rows.Add(4, "DD", "dd")
            dt.Rows.Add(5, "EE", "ee")
    
            DataGridView1.DataSource = dt
    
            Dim dt1 As New DataTable
            dt1.Columns.Add("Column1", GetType(Int32))
            dt1.Columns.Add("Column2", GetType(String))
            dt1.Columns.Add("Column3", GetType(String))
    
            dt1.Rows.Add(6, "HH", "hh")
            dt1.Rows.Add(7, "FF", "ff")
            dt1.Rows.Add(3, "CC", "cc")
            dt1.Rows.Add(4, "GG", "gg")
            dt1.Rows.Add(9, "EE", "ee")
    
            DataGridView2.DataSource = dt1
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            For i As Integer = 0 To DataGridView1.Rows.Count - 1
                DataGridView3.Rows.Add()
    
                For j As Integer = 0 To DataGridView1.Rows(i).Cells.Count - 1
                    DataGridView3.Rows(i).Cells(j).Value = DataGridView1.Rows(i).Cells(j).Value
                Next
            Next
            Dim rowindex As Integer = DataGridView1.Rows.Count - 1
            For i As Integer = 0 To DataGridView2.Rows.Count - 1
                DataGridView3.Rows.Add()
                rowindex = rowindex + 1
    
                For j As Integer = 0 To DataGridView2.Rows(i).Cells.Count - 1
                    DataGridView3.Rows(rowindex).Cells(j).Value = DataGridView2.Rows(i).Cells(j).Value
                Next
            Next
    
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            For i As Integer = 0 To Me.DataGridView3.RowCount - 2
                For j As Integer = i + 1 To Me.DataGridView3.RowCount - 2
                    If DataGridView3.Rows(i).Cells(0).Value = DataGridView3.Rows(j).Cells(0).Value Then
    
                        DataGridView3.Rows.Remove(DataGridView3.Rows(i))
                        i -= 1
                        Debug.Print("duplicate value " & DataGridView3.Rows(i).Cells(0).Value)
                        'DataGridView1.Rows(i).Cells(1).Value = "Duplicate"
                    End If
                Next
            Next
        End Sub

    Copy the data from datagridview1 and datagridview2 into datagridview3 in Button1_click event, remove the duplicate item from datagridview3 in Button2_click event.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Mike Zed Monday, September 4, 2017 6:48 PM
    Monday, September 4, 2017 9:13 AM
    Moderator
  • Dear Acamar,

    thank you very much for your reply.

    :)

    Monday, September 4, 2017 6:57 PM
  • Dear Cherry,

    Thank you very much for your reply.
    that's like i mean.
    i'll try to run that code in my app.

    Warm regards,
    HMZ


    Monday, September 4, 2017 7:03 PM
  • Dear Cherry,

    when i run the code
    i get an error msg like;

    "An unhandled exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll

    Additional information: No row can be added to a DataGridView control that does not have columns. Columns must be added first."

    what should i do?

    thnx
    Tuesday, September 5, 2017 8:09 AM
  • Dear Cherry,

    when i run the code
    i get an error msg like;

    "An unhandled exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll

    Additional information: No row can be added to a DataGridView control that does not have columns. Columns must be added first."

    what should i do?

    thnx

    Hi Mike,

    According to your description, do you add some column in your datagridview3 control, if not, you will encounter this issue.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, September 5, 2017 8:15 AM
    Moderator
  • Dear Cheery,

    how to add the columns in datagridview3.
    may i add in property component?

    thnx
    Tuesday, September 5, 2017 8:40 AM
  • Dear Cherry,

    the issue has solved, after i add columns in datagridview3.
    many thank for your help.

    cheers,
    HMZ
    Tuesday, September 5, 2017 8:42 AM