Results 1 to 29 of 29

Thread: [RESOLVED] Tranfer and delete data from DataGridView1 to DataGridView2

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2015
    Location
    ANTANANARIVO
    Posts
    464

    Resolved [RESOLVED] Tranfer and delete data from DataGridView1 to DataGridView2

    Hello VBForums
    Hello eveyone
    Please Gentelmans ..
    If you can help to resolve this problem ..
    In my Form1 i have DataGridView1 with column0 "CheckBox" .. and DataGridView2 .. Button1
    With this code when i click on Button1 all data in DataGridView1 will be transferred from DataGridView1 to DataGridView2
    How to make for transfer " cut " only the checked line ..
    The transferred line will be removed from the DataGridView1
    My Code in Form Load :
    Code:
        AddCheckBox()
    Code:
       Sub AddCheckBox()
            Dim field As New DataGridViewCheckBoxColumn
            field.HeaderText = "CHECKED"
            DataGridView1.Columns.Insert(0, field)
            DataGridView1.Columns(0).Width = 130
        End Sub
    Code:
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim sourceGrid As DataGridView = Me.DataGridView1
            Dim targetGrid As DataGridView = Me.DataGridView2
            'Copy all rows and cells.
            Dim targetRows = New List(Of DataGridViewRow)
            For Each sourceRow As DataGridViewRow In sourceGrid.Rows
                If (Not sourceRow.IsNewRow) Then
                    Dim targetRow = CType(sourceRow.Clone(), DataGridViewRow)
                    For Each cell As DataGridViewCell In sourceRow.Cells
                        targetRow.Cells(cell.ColumnIndex).Value = cell.Value
                    Next
                    targetRows.Add(targetRow)
                End If
            Next
            targetGrid.Columns.Clear()
            For Each column As DataGridViewColumn In sourceGrid.Columns
                targetGrid.Columns.Add(CType(column.Clone(), DataGridViewColumn))
            Next
            targetGrid.Rows.AddRange(targetRows.ToArray())
        End Sub
    Thank you in advance for help
    Cordially
    MADA

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,296

    Re: Tranfer and delete data from DataGridView1 to DataGridView2

    You already have an If statement in their to avoid copying the new row at the bottom of the grid. Why can't you have an If statement to avoid copying rows that aren't checked in that column?

  3. #3
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: Tranfer and delete data from DataGridView1 to DataGridView2

    No time to explain whats going on here but maybe youll see it and work something out

    Code:
    Public Class DGVRowSize
        Dim dt1, dt2 As New DataTable
        Dim BS As New BindingSource
        Dim DV As DataView
        Private Sub ButtonAddRow_Click(sender As Object, e As EventArgs) Handles ButtonAddRow.Click
            With dt1
                .Columns.Add("Select", GetType(System.Boolean))
                .Columns.Add("Col1", GetType(System.String))
                .Columns("Select").DefaultValue = False
            End With
            BS.DataSource = dt1
    
            For i As Integer = 0 To 100
                BS.AddNew()
                CType(BS.Current, DataRowView)("Select") = False
                CType(BS.Current, DataRowView)("Col1") = "Value" & i.ToString
                BS.EndEdit()
            Next
    
            dt2.Merge(dt1)
    
            With DataGridView1
                .DataSource = BS            
            End With
    
    
            DV = dt2.DefaultView
    
            DV.RowFilter = "Select=True"
    
            With DataGridView2
                .AllowUserToAddRows = False
                .DataSource = DV
            End With
    
        End Sub
    
        Private Sub DataGridView1_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles DataGridView1.CurrentCellDirtyStateChanged
            DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
        End Sub
    
        Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
            If DataGridView1.CurrentCell.OwningColumn.Name = "Select" Then
                BS.EndEdit()
                dt2.Rows.Clear()
                dt2.Merge(dt1)
            End If
        End Sub
    End Class

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2015
    Location
    ANTANANARIVO
    Posts
    464

    Re: Tranfer and delete data from DataGridView1 to DataGridView2

    Quote Originally Posted by jmcilhinney View Post
    You already have an If statement in their to avoid copying the new row at the bottom of the grid. Why can't you have an If statement to avoid copying rows that aren't checked in that column?
    Hiii jmcilhinney
    Thank you very much for your advice .. I am very beginner in this field I could not avoid this " IF " .. how to make please to transfer only the lines checked
    Thank you in advance
    Cordially
    MADA

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2015
    Location
    ANTANANARIVO
    Posts
    464

    Re: Tranfer and delete data from DataGridView1 to DataGridView2

    Quote Originally Posted by kpmc View Post
    No time to explain whats going on here but maybe youll see it and work something out

    Code:
    Public Class DGVRowSize
        Dim dt1, dt2 As New DataTable
        Dim BS As New BindingSource
        Dim DV As DataView
        Private Sub ButtonAddRow_Click(sender As Object, e As EventArgs) Handles ButtonAddRow.Click
            With dt1
                .Columns.Add("Select", GetType(System.Boolean))
                .Columns.Add("Col1", GetType(System.String))
                .Columns("Select").DefaultValue = False
            End With
            BS.DataSource = dt1
    
            For i As Integer = 0 To 100
                BS.AddNew()
                CType(BS.Current, DataRowView)("Select") = False
                CType(BS.Current, DataRowView)("Col1") = "Value" & i.ToString
                BS.EndEdit()
            Next
    
            dt2.Merge(dt1)
    
            With DataGridView1
                .DataSource = BS            
            End With
    
    
            DV = dt2.DefaultView
    
            DV.RowFilter = "Select=True"
    
            With DataGridView2
                .AllowUserToAddRows = False
                .DataSource = DV
            End With
    
        End Sub
    
        Private Sub DataGridView1_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles DataGridView1.CurrentCellDirtyStateChanged
            DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
        End Sub
    
        Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
            If DataGridView1.CurrentCell.OwningColumn.Name = "Select" Then
                BS.EndEdit()
                dt2.Rows.Clear()
                dt2.Merge(dt1)
            End If
        End Sub
    End Class
    Thank you kpmc
    Frankly I could not adapt this code in my Form
    In any case thank you gentelman
    Cordially
    MADA

  6. #6
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: Tranfer and delete data from DataGridView1 to DataGridView2

    Quote Originally Posted by MADA BLACK View Post
    Thank you kpmc
    Frankly I could not adapt this code in my Form
    In any case thank you gentelman
    Cordially
    MADA
    Show me your fill procedure on DataGridView1

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2015
    Location
    ANTANANARIVO
    Posts
    464

    Re: Tranfer and delete data from DataGridView1 to DataGridView2

    Quote Originally Posted by kpmc View Post
    Show me your fill procedure on DataGridView1
    Hiii kpmc
    With another Module only for connection :
    Code:
    Imports System.Data
    Imports System.Data.OleDb
    Imports System.IO
        Public Class Form1
            Dim InfoConnection As OleDbConnection
            Dim InfoCommand As OleDbCommand
            Dim InfoAdapter As OleDbDataAdapter
            Dim InfoTable As DataTable
            Dim InfoManager As CurrencyManager
            Dim InfoState As String
            Dim InfoBookmark As Integer
            Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
                connectiondatabase()
                InfoCommand = New OleDbCommand("SELECT * FROM INVOICE ORDER BY CUSTOMER", con)
                InfoAdapter = New OleDbDataAdapter()
                InfoAdapter.SelectCommand = InfoCommand
                InfoTable = New DataTable()
                InfoAdapter.Fill(InfoTable)
                DataGridView1.DataSource = InfoTable
                InfoManager = DirectCast(Me.BindingContext(InfoTable), CurrencyManager)
                With DataGridView1
                    DataGridView1.Columns(0).HeaderText = "ID"
                    DataGridView1.Columns(1).HeaderText = "CODE"
                    DataGridView1.Columns(2).HeaderText = "CUSTOMERNAME"
                    DataGridView1.Columns(3).HeaderText = "CUSTOMERADRESSE"
                End With
            End Sub
    End Class
    Cordially
    MADA

  8. #8
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: Tranfer and delete data from DataGridView1 to DataGridView2

    First what I would do is create a columns string for your Command text such as "Select SomeLongID as ID, SomeLongCode as Code " etc and remove the headertext renaming methods

    Look at the code I shared and just replace "DT1" with "InfoTable" and add the "Select" Column to it after the fill. Then change it's displayindex to 0
    When you check this value it will cause the second DataGrideView to show only that row, and unchecking it will remove it from the DGV

    Got a meeting to hit right now or Id give you a better example

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2015
    Location
    ANTANANARIVO
    Posts
    464

    Re: Tranfer and delete data from DataGridView1 to DataGridView2

    Hiii kpmc
    Please ..
    I could not understand and settle this line :
    " .....add the "Select" Column to it after the fill. Then change it's displayindex to 0 .... "
    Here is the complete code in my form .. please if you can see how to solve it
    Code:
    Imports System.Data
    Imports System.Data.OleDb
    Imports System.IO
    Public Class Form1
        Dim dt1, dt2 As New DataTable
        Dim BS As New BindingSource
        Dim DV As DataView
        Dim InfoConnection As OleDbConnection
        Dim InfoCommand As OleDbCommand
        Dim InfoAdapter As OleDbDataAdapter
        Dim InfoTable As DataTable
        Dim InfoManager As CurrencyManager
        Dim InfoState As String
        Dim InfoBookmark As Integer
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            connectiondatabase()
            InfoCommand = New OleDbCommand("SELECT * FROM INVOICE ORDER BY CUSTOMER", con)
            InfoAdapter = New OleDbDataAdapter()
            InfoAdapter.SelectCommand = InfoCommand
            InfoTable = New DataTable()
            InfoAdapter.Fill(InfoTable)
            DataGridView1.DataSource = InfoTable
            InfoManager = DirectCast(Me.BindingContext(InfoTable), CurrencyManager)
            With DataGridView1
                DataGridView1.Columns(0).HeaderText = "ID"
                DataGridView1.Columns(1).HeaderText = "CODE"
                DataGridView1.Columns(2).HeaderText = "CUSTOMERNAME"
                DataGridView1.Columns(3).HeaderText = "CUSTOMERADRESSE"
            End With
            AddCheckBox()
        End Sub
        'Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        '    Dim sourceGrid As DataGridView = Me.DataGridView1
        '    Dim targetGrid As DataGridView = Me.DataGridView2
        '    Dim targetRows = New List(Of DataGridViewRow)
        '    For Each sourceRow As DataGridViewRow In sourceGrid.Rows
        '        If (Not sourceRow.IsNewRow) Then
        '            Dim targetRow = CType(sourceRow.Clone(), DataGridViewRow)
        '            For Each cell As DataGridViewCell In sourceRow.Cells
        '                targetRow.Cells(cell.ColumnIndex).Value = cell.Value
        '            Next
        '            targetRows.Add(targetRow)
        '        End If
        '    Next
        '    targetGrid.Columns.Clear()
        '    For Each column As DataGridViewColumn In sourceGrid.Columns
        '        targetGrid.Columns.Add(CType(column.Clone(), DataGridViewColumn))
        '    Next
        '    targetGrid.Rows.AddRange(targetRows.ToArray())
        'End Sub
        Sub AddCheckBox()
            Dim field As New DataGridViewCheckBoxColumn
            field.HeaderText = "CHECKED"
            DataGridView1.Columns.Insert(0, field)
            DataGridView1.Columns(0).Width = 130
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            With InfoTable
                .Columns.Add("Select", GetType(System.Boolean))
                .Columns.Add("Col1", GetType(System.String))
                .Columns("Select").DefaultValue = False
            End With
            BS.DataSource = InfoTable
            For i As Integer = 0 To 100
                BS.AddNew()
                CType(BS.Current, DataRowView)("Select") = False
                CType(BS.Current, DataRowView)("Col1") = "Value" & i.ToString
                BS.EndEdit()
            Next
            dt2.Merge(InfoTable)
            With DataGridView1
                .DataSource = BS
            End With
            DV = dt2.DefaultView
            DV.RowFilter = "Select=True"
            With DataGridView2
                .AllowUserToAddRows = False
                .DataSource = DV
            End With
        End Sub
        Private Sub DataGridView1_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles DataGridView1.CurrentCellDirtyStateChanged
            DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
        End Sub
        Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
            If DataGridView1.CurrentCell.OwningColumn.Name = "Select" Then
                BS.EndEdit()
                dt2.Rows.Clear()
                dt2.Merge(InfoTable)
            End If
        End Sub
    End Class
    Cordially
    MADA

  10. #10
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: Tranfer and delete data from DataGridView1 to DataGridView2

    Something closer to this

    Code:
    Imports System.Data.OleDb
    Public Class DGVRowSize
        Dim InfoTable, dt2 As New DataTable
        Dim BS As New BindingSource
        Dim DV As DataView
    
        Private Sub DGVRowSize_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'I dont know what you got going on in "connectiondatabase()"
            Using OLEDBconn As New OleDbConnection("Your Connection String")
                Dim Cols As String = "YourIDCol as ID, YourCodeCol As CODE, YourCusomterName As CUSTOMERNAME, YourCustomerAddress As CUSTOMERADRESSE"
                Using InfoTableDA As New OleDbDataAdapter("SELECT " & Cols & " FROM INVOICE ORDER BY CUSTOMER", OLEDBconn)
                    InfoTableDA.Fill(InfoTable)
    
    
                    With InfoTable
                        .Columns.Add("Select", GetType(System.Boolean))
                        .Columns("Select").DefaultValue = False
                        BS.DataSource = InfoTable
    
                        With DataGridView1
                            .DataSource = BS
                            .Columns("Select").DisplayIndex = 0
                        End With
    
                    End With
    
                    dt2.Merge(InfoTable)
                    DV = dt2.DefaultView
                    DV.RowFilter = "Select=True"
    
                    With DataGridView2
                        .DataSource = DV
                        .Columns("Select").Visible = False
                        .AllowUserToAddRows = False
                    End With
    
                End Using
            End Using
    
        End Sub
    
        Private Sub DataGridView1_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles DataGridView1.CurrentCellDirtyStateChanged
            DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
        End Sub
    
        Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
            If DataGridView1.CurrentCell.OwningColumn.Name = "Select" Then
                BS.EndEdit()
                dt2.Rows.Clear()
                dt2.Merge(InfoTable)
            End If
        End Sub
    End Class

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2015
    Location
    ANTANANARIVO
    Posts
    464

    Re: Tranfer and delete data from DataGridView1 to DataGridView2

    Thank you kpmc
    Thank you for all
    I will try it
    This is in my Module 1 :
    Code:
    Imports System.Data
    Imports System.Data.OleDb
    Module Module1
        Public con As New OleDbConnection
        Public Adapt As New OleDbDataAdapter
        Public dt As New DataTable
        Public Sub connectiondatabase()
            If con.State = ConnectionState.Open Then con.Close()
            Try
                con = New OleDbConnection("provider=Microsoft.Ace.OLEDB.12.0;Data Source=|datadirectory|\DATASTOCK.accdb ;Jet OLEDB:Database Password=KPMC")
                con.Open()
            Catch ex As Exception
                MessageBox.Show(ex.Message, "The database is not connected", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly)
                con.Close()
                End
            End Try
        End Sub
    End Module
    Cordially
    MADA

  12. #12
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: Tranfer and delete data from DataGridView1 to DataGridView2

    Unless you have a specific reason for the module, I would dump it

    replace
    Code:
    Using OLEDBconn As New OleDbConnection("Your Connection String")
    with
    Code:
    Using OLEDBconn As New OleDbConnection("provider=Microsoft.Ace.OLEDB.12.0;Data Source=|datadirectory|\DATASTOCK.accdb ;Jet OLEDB:Database Password=KPMC")
    in the code i showed

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2015
    Location
    ANTANANARIVO
    Posts
    464

    Re: Tranfer and delete data from DataGridView1 to DataGridView2

    Thank you master for your good follow for this subject
    I followed all the instructions
    Now i have this error in this line ..
    InfoTableDA.Fill(InfoTable) ......"No value given for one or more of the required parameters."
    Cordially
    MADA

  14. #14
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: Tranfer and delete data from DataGridView1 to DataGridView2

    paste your code please

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2015
    Location
    ANTANANARIVO
    Posts
    464

    Re: Tranfer and delete data from DataGridView1 to DataGridView2

    Sorry KPMC .. it's my fault
    I corrected the problem
    Thank you very much for your impeccable and great code
    It works very very well
    I only have one small remark ..
    How to clear the tranfered line from DataGrid1
    Cordially
    MADA

  16. #16
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: Tranfer and delete data from DataGridView1 to DataGridView2

    Code:
        Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
            If DataGridView1.CurrentCell.OwningColumn.Name = "Select" Then
                BS.EndEdit()
                dt2.Rows.Clear()
                dt2.Merge(InfoTable)
                BS.RemoveCurrent()
                BS.EndEdit()
            End If
    
        End Sub

  17. #17
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: Tranfer and delete data from DataGridView1 to DataGridView2

    Refresh your browser and make sure youre looking at the latest edit in my code

  18. #18
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: Tranfer and delete data from DataGridView1 to DataGridView2

    Thinking about this more, removing the row like this will remove it from dt2 as well.

    What may work is creating a rowfilter on the default view of InfoTable

    InfoTable.DefaultView.RowFilter="Select=False"

  19. #19

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2015
    Location
    ANTANANARIVO
    Posts
    464

    Re: Tranfer and delete data from DataGridView1 to DataGridView2

    Hiii kpmc
    Your code works very very well .. thank you very well
    And that's what I want to see and have
    Only when I check a line in DataGrid1 will automatically be transferred to the DataGrid2 ..
    Until here it works very well ..
    The problem always i have a single line in DataGrid2
    Normally I have in DataGrid1 for example 15 line
    When I transfer 8 line then in DataGrid2 will be displayed 8 line .. i transfer from DataGrid1 5 line will be displayed in DataGrid2 5 line ..etc
    This is the last code in my Form 1 :
    Code:
    Imports System.Data.OleDb
    Public Class DGVRowSize
        Dim InfoTable, dt2 As New DataTable
        Dim BS As New BindingSource
        Dim DV As DataView
        Private Sub DGVRowSize_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Using OLEDBconn As New OleDbConnection("provider=Microsoft.Ace.OLEDB.12.0;Data Source=|datadirectory|\DATASTOCK.accdb ;Jet OLEDB:Database Password=KPMC")
                Dim Cols As String = "ID as ID, CODE As CODE, CUSTOMERNAME As CUSTOMERNAME, CUSTOMERADRESSE As CUSTOMERADRESSE"
                Using InfoTableDA As New OleDbDataAdapter("SELECT " & Cols & " FROM INVOICE ORDER BY CUSTOMER", OLEDBconn)
                    InfoTableDA.Fill(InfoTable)
                    With InfoTable
                        .Columns.Add("Select", GetType(System.Boolean))
                        .Columns("Select").DefaultValue = False
                        BS.DataSource = InfoTable
                        With DataGridView1
                            .DataSource = BS
                            .Columns("Select").DisplayIndex = 0
                        End With
                    End With
                    dt2.Merge(InfoTable)
                    DV = dt2.DefaultView
                    DV.RowFilter = "Select=True"
                    With DataGridView2
                        .DataSource = DV
                        .Columns("Select").Visible = False
                        .AllowUserToAddRows = False
                    End With
                End Using
            End Using
        End Sub
        Private Sub DataGridView1_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles DataGridView1.CurrentCellDirtyStateChanged
            DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
        End Sub
        Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
            If DataGridView1.CurrentCell.OwningColumn.Name = "Select" Then
                BS.EndEdit()
                dt2.Rows.Clear()
                dt2.Merge(InfoTable)
                BS.RemoveCurrent()
                BS.EndEdit()
            End If
        End Sub
    End Class
    Than you a lot gentelman
    MADA

  20. #20
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: Tranfer and delete data from DataGridView1 to DataGridView2

    remove this
    Code:
                BS.RemoveCurrent()
                BS.EndEdit()
    Code:
                    dt2.Merge(InfoTable)
                    InfoTable.DefaultView.RowFilter="Select=False" '<--add this
                    DV = dt2.DefaultView
                    DV.RowFilter = "Select=True"

  21. #21

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2015
    Location
    ANTANANARIVO
    Posts
    464

    Re: Tranfer and delete data from DataGridView1 to DataGridView2

    Dear kpmc ..
    Sorry ..
    Like this :
    Code:
        Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
            If DataGridView1.CurrentCell.OwningColumn.Name = "Select" Then
                BS.EndEdit()
                dt2.Rows.Clear()
                dt2.Merge(InfoTable)
    
                ''BS.RemoveCurrent()
                ''BS.EndEdit()
    
                dt2.Merge(InfoTable)
                InfoTable.DefaultView.RowFilter = "Select=False" '<--add this
                DV = dt2.DefaultView
                DV.RowFilter = "Select=True"
            End If
        End Sub
    Does not work very well !!
    Cordially
    MADA

  22. #22
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: Tranfer and delete data from DataGridView1 to DataGridView2

    No, add it in the load event in relation to where i added it in post#20

    Code:
                    dt2.Merge(InfoTable)
                    InfoTable.DefaultView.RowFilter="Select=False" '<--add this
                    DV = dt2.DefaultView
                    DV.RowFilter = "Select=True"

  23. #23
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: Tranfer and delete data from DataGridView1 to DataGridView2

    To be clear your code should look like this

    Code:
    Imports System.Data.OleDb
    Public Class DGVRowSize
        Dim InfoTable, dt2 As New DataTable
        Dim BS As New BindingSource
        Dim DV As DataView
        Private Sub DGVRowSize_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Using OLEDBconn As New OleDbConnection("provider=Microsoft.Ace.OLEDB.12.0;Data Source=|datadirectory|\DATASTOCK.accdb ;Jet OLEDB:Database Password=KPMC")
                Dim Cols As String = "ID as ID, CODE As CODE, CUSTOMERNAME As CUSTOMERNAME, CUSTOMERADRESSE As CUSTOMERADRESSE"
                Using InfoTableDA As New OleDbDataAdapter("SELECT " & Cols & " FROM INVOICE ORDER BY CUSTOMER", OLEDBconn)
                    InfoTableDA.Fill(InfoTable)
                    With InfoTable
                        .Columns.Add("Select", GetType(System.Boolean))
                        .Columns("Select").DefaultValue = False
                        BS.DataSource = InfoTable
                        With DataGridView1
                            .DataSource = BS
                            .Columns("Select").DisplayIndex = 0
                        End With
                    End With
                    dt2.Merge(InfoTable)
                    DV = dt2.DefaultView
                    InfoTable.DefaultView.RowFilter="Select=False"
                    DV.RowFilter = "Select=True"
                    With DataGridView2
                        .DataSource = DV
                        .Columns("Select").Visible = False
                        .AllowUserToAddRows = False
                    End With
                End Using
            End Using
        End Sub
        Private Sub DataGridView1_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles DataGridView1.CurrentCellDirtyStateChanged
            DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
        End Sub
        Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
            If DataGridView1.CurrentCell.OwningColumn.Name = "Select" Then
                BS.EndEdit()
                dt2.Rows.Clear()
                dt2.Merge(InfoTable)
            End If
        End Sub
    End Class

  24. #24

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2015
    Location
    ANTANANARIVO
    Posts
    464

    Re: Tranfer and delete data from DataGridView1 to DataGridView2

    Thank you kpmc
    But now when i open my Form1 .. the DataGrid1 is completely empty ..
    Because this line :
    InfoTable.DefaultView.RowFilter = "Select=False"
    MADA

  25. #25
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: Tranfer and delete data from DataGridView1 to DataGridView2

    Put this right after you Set the BS.DataSource=InfoTable
    Code:
                        For Each DRV As DataRowView In BS
                            DRV("Select") = False
                        Next
    Or you could try to add the column to the datatable before you fill it, thus it would default to =False as its being filled

  26. #26
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: Tranfer and delete data from DataGridView1 to DataGridView2

    Or you could probably do this
    InfoTable.DefaultView.RowFilter = "Select=False Or Select Is Null"

  27. #27
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: Tranfer and delete data from DataGridView1 to DataGridView2

    After all this it maybe worth keeping the Select Column Visible in dgv2 and creating a similar CellCOntentClick sub which could "move" the row back to dgv1

  28. #28

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2015
    Location
    ANTANANARIVO
    Posts
    464

    Re: Tranfer and delete data from DataGridView1 to DataGridView2

    Gentelman kpmc
    From my heart " THANK YOU VERY MUCH "
    Perfectly resolved
    Cordially
    MADA BLACK
    Last edited by MADA BLACK; Feb 22nd, 2018 at 04:21 PM.

  29. #29
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: [RESOLVED] Tranfer and delete data from DataGridView1 to DataGridView2

    Happy to help my friend.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width