Results 1 to 13 of 13

Thread: Fill dataGridView from DataGridView

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2009
    Posts
    56

    Fill dataGridView from DataGridView

    Hi,

    I'm looking for a code, to Fill a dataGridView from DataGridView by clicking on a row.

    This is what I have til now

    Code:
    Imports System.Data.SQLClient
    
    Public Class hulBereken2
    
        Private Sub hulBereken2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            GridOphalen()
        End Sub
    
        Private Sub GridOphalen()
    
            Dim connectionString As String = My.Settings.HulpstoffenConnectionString()
            Dim SQLString As String = "Select * From Hulpstof"
            Dim mijnDataAdapter As New SqlDataAdapter(SQLString, connectionString)
            Dim mijnDataSet As New DataSet
    
            mijnDataAdapter.Fill(mijnDataSet)
            DataGridView1.DataSource = mijnDataSet.Tables(0)
        End Sub
    
        Private Sub DataGridView1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseUp
            Dim cm As CurrencyManager = CType(Me.BindingContext(DataGridView1.DataSource, DataGridView1.DataMember), CurrencyManager)
            Dim dv As DataView = CType(cm.List, DataView)
            Dim dr As DataRow
    
            dr = dv.Item(cm.Position).Row
    
    
    
          
    
    
    
        End Sub
    End Class
    thanks,

    Fred from Holland

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Fill dataGridView from DataGridView

    I'm not sure what you are trying to do. You mention filling a DGV from a DGV, and show a bit of code, but that code doesn't make it clear that there is a second DGV. It also doesn't suggest what the second DGV should be showing. Should it show some set of records filtered by whatever was selected in the first DGV? Should it just take in the record from the first DGV? etc.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2009
    Posts
    56

    Re: Fill dataGridView from DataGridView

    Quote Originally Posted by Shaggy Hiker View Post
    I'm not sure what you are trying to do. You mention filling a DGV from a DGV, and show a bit of code, but that code doesn't make it clear that there is a second DGV. It also doesn't suggest what the second DGV should be showing. Should it show some set of records filtered by whatever was selected in the first DGV? Should it just take in the record from the first DGV? etc.
    sorry for the bad explanation

    I need the rest of the code for show in the second DGV, some set of records filtered by whatever was selected in the first DGV, as you say.
    The code i showing, is a start, more i dont know

    gr Fred

  4. #4
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: Fill dataGridView from DataGridView

    This should do the trick for you:
    This creates a new DataTable and adds the SelectedRows from DGV1 to the Table, then you bind the DGV2 to the new Table

    Code:
     Dim dt As New DataTable
            For i As Integer = 0 To Me.DataGridView1.ColumnCount - 1
                dt.Columns.Add(Me.DataGridView1.Columns(i).HeaderText, Me.DataGridView1.Columns(i).ValueType)
    
            Next
    
            Dim dr As DataRow = dt.NewRow()
            For i As Integer = 0 To Me.DataGridView1.ColumnCount - 1
                dr.Item(i) = Me.DataGridView1.SelectedRows.Item(0).Cells(i).Value
    
            Next
    
            dt.Rows.Add(dr)
            Me.DataGridView2.DataSource = dt
    Let me know if this works for you?

    Just discovered this is only adding ONE selected row, not multiple. Stay tuned.....
    Last edited by billboy; Nov 4th, 2011 at 07:34 PM.

  5. #5
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: Fill dataGridView from DataGridView

    Revised code to retirve ALL selected rows

    Code:
    Dim dt As New DataTable
    
            For i As Integer = 0 To Me.DataGridView1.ColumnCount - 1
                dt.Columns.Add(Me.DataGridView1.Columns(i).HeaderText, Me.DataGridView1.Columns(i).ValueType)
            Next
            For r = Me.DataGridView1.SelectedRows.Count - 1 To 0 Step -1
                Dim dr As DataRow = dt.NewRow()
                For i As Integer = 0 To Me.DataGridView1.ColumnCount - 1
                    dr.Item(i) = Me.DataGridView1.SelectedRows.Item(r).Cells(i).Value
                Next
                dt.Rows.Add(dr)
            Next
            Me.DataGridView2.DataSource = dt
    Hope this helps, please let me know

  6. #6

    Thread Starter
    Member
    Join Date
    Jul 2009
    Posts
    56

    Re: Fill dataGridView from DataGridView

    Quote Originally Posted by billboy View Post
    Revised code to retirve ALL selected rows

    Code:
    Dim dt As New DataTable
    
            For i As Integer = 0 To Me.DataGridView1.ColumnCount - 1
                dt.Columns.Add(Me.DataGridView1.Columns(i).HeaderText, Me.DataGridView1.Columns(i).ValueType)
            Next
            For r = Me.DataGridView1.SelectedRows.Count - 1 To 0 Step -1
                Dim dr As DataRow = dt.NewRow()
                For i As Integer = 0 To Me.DataGridView1.ColumnCount - 1
                    dr.Item(i) = Me.DataGridView1.SelectedRows.Item(r).Cells(i).Value
                Next
                dt.Rows.Add(dr)
            Next
            Me.DataGridView2.DataSource = dt
    Hope this helps, please let me know
    Hi,

    "r" was not declared, so i use Integer

    now i have , by clicking one a row, by DataGridView1, the first row is filling from datagridview2.

    When i click on the second one, he overwrite de first one, i want that he filling the second row

    Stil not working, look for the code please

    thanks,
    Fred

    Code:
    
    Private Sub DataGridView1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseUp
    
            Dim dt As New DataTable
    
            For i As Integer = 0 To Me.DataGridView1.ColumnCount - 1
                dt.Columns.Add(Me.DataGridView1.Columns(i).HeaderText, Me.DataGridView1.Columns(i).ValueType)
            Next
            For r As Integer = Me.DataGridView1.SelectedRows.Count - 1 To 0 Step -1
                Dim dr As DataRow = dt.NewRow()
                For i As Integer = 0 To Me.DataGridView1.ColumnCount - 1
                    dr.Item(i) = Me.DataGridView1.SelectedRows.Item(r).Cells(i).Value
                Next
                dt.Rows.Add(dr)
            Next
            Me.DataGridView2.DataSource = dt
    
    
    
    
        End Sub
    Last edited by freddiebunt; Nov 5th, 2011 at 08:04 AM. Reason: .

  7. #7
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Fill dataGridView from DataGridView

    The attached VS2008 project loads XML data to a DataTable which becomes the DataSource for a BindingSource which in turn becomes the DataSource of a DataGridView. So in short we work off the top DataGridView bound to a DataTable via a BindingSource.

    Code:
        Private Sub DataGridView1_MouseDoubleClick() Handles TopGrid.MouseDoubleClick
            bsCustomers.CloneCurrentRow(bsCustomersClone, "Identifier", True, True)
        End Sub
    In the code above a DataRow is added from the top to bottom DataGridView which prevents duplicate rows being added to the bottom DataGridView.

    Language extension method to clone row
    Code:
    <System.Runtime.CompilerServices.Extension()> _
    Public Sub CloneCurrentRow( _
        ByVal Source As BindingSource, _
        ByVal Target As BindingSource, _
        ByVal KeyField As String, _
        ByVal Reposition As Boolean, _
        ByVal CloneColumns As Boolean)
    
        If Source.IsExistingRow Then
            If CloneColumns Then
                If Target.DataTable.Columns.Count <> Source.DataTable.Columns.Count Then
                    Target.DataSource = Source.DataTable.Clone
                End If
            End If
        End If
        If IsDataRow(Source) Then
            Dim Position As Integer = Target.Find(KeyField, Source.CurrentRow(KeyField))
            If Position = -1 Then
                Target.DataTable.ImportRow(Source.CurrentRow)
            Else
                If Reposition Then
                    Target.Position = Position
                End If
            End If
        End If
    End Sub
    Attached Files Attached Files

  8. #8

    Thread Starter
    Member
    Join Date
    Jul 2009
    Posts
    56

    Re: Fill dataGridView from DataGridView

    Quote Originally Posted by kevininstructor View Post
    The attached VS2008 project loads XML data to a DataTable which becomes the DataSource for a BindingSource which in turn becomes the DataSource of a DataGridView. So in short we work off the top DataGridView bound to a DataTable via a BindingSource.

    Code:
        Private Sub DataGridView1_MouseDoubleClick() Handles TopGrid.MouseDoubleClick
            bsCustomers.CloneCurrentRow(bsCustomersClone, "Identifier", True, True)
        End Sub
    In the code above a DataRow is added from the top to bottom DataGridView which prevents duplicate rows being added to the bottom DataGridView.

    Language extension method to clone row
    Code:
    <System.Runtime.CompilerServices.Extension()> _
    Public Sub CloneCurrentRow( _
        ByVal Source As BindingSource, _
        ByVal Target As BindingSource, _
        ByVal KeyField As String, _
        ByVal Reposition As Boolean, _
        ByVal CloneColumns As Boolean)
    
        If Source.IsExistingRow Then
            If CloneColumns Then
                If Target.DataTable.Columns.Count <> Source.DataTable.Columns.Count Then
                    Target.DataSource = Source.DataTable.Clone
                End If
            End If
        End If
        If IsDataRow(Source) Then
            Dim Position As Integer = Target.Find(KeyField, Source.CurrentRow(KeyField))
            If Position = -1 Then
                Target.DataTable.ImportRow(Source.CurrentRow)
            Else
                If Reposition Then
                    Target.Position = Position
                End If
            End If
        End If
    End Sub
    thanks, but i use now VB2005 for my project

  9. #9
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: Fill dataGridView from DataGridView

    This wont prevent a duplicate row,

    Code:
    If dt.Rows.Count <= 0 Then
                For i As Integer = 0 To Me.DataGridView1.ColumnCount - 1
                    dt.Columns.Add(Me.DataGridView1.Columns(i).HeaderText, Me.DataGridView1.Columns(i).ValueType)
                Next
                For r As Integer = Me.DataGridView1.SelectedRows.Count - 1 To 0 Step -1
                    Dim dr As DataRow = dt.NewRow()
                    For i As Integer = 0 To Me.DataGridView1.ColumnCount - 1
                        dr.Item(i) = Me.DataGridView1.SelectedRows.Item(r).Cells(i).Value
                    Next
                    dt.Rows.Add(dr)
                Next
            ElseIf dt.Rows.Count >= 1 Then
                For r As Integer = Me.DataGridView1.SelectedRows.Count - 1 To 0 Step -1
                    Dim dr As DataRow = dt.NewRow()
                    For i As Integer = 0 To Me.DataGridView1.ColumnCount - 1
                        dr.Item(i) = Me.DataGridView1.SelectedRows.Item(r).Cells(i).Value
                    Next
                    dt.Rows.Add(dr)
                Next
            End If
            Me.DataGridView2.DataSource = dt

  10. #10
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Fill dataGridView from DataGridView

    Quote Originally Posted by freddiebunt View Post
    thanks, but i use now VB2005 for my project
    Yeah I did not notice that but the code can still work if you are willing to put the effort in.

    For example, in the following code VS2008 syntax is commented out while VS2005 syntax is used.
    Code:
        Private Sub DataGridView1_MouseDoubleClick() Handles TopGrid.MouseDoubleClick
            'bsCustomers.CloneCurrentRow(bsCustomersClone, "Identifier", True, True)
            CloneCurrentRow(bsCustomers, bsCustomersClone, "Identifier", True, True)
        End Sub
    Then we would tweak the CloneCurrentRow into a regular procedure as follows
    Code:
    Module BindingSourceExtensions
        Public Sub CloneCurrentRow( _
            ByVal Source As BindingSource, _
            ByVal Target As BindingSource, _
            ByVal KeyField As String, _
            ByVal Reposition As Boolean, _
            ByVal CloneColumns As Boolean)
    
            If DirectCast(Source.Current, DataRowView).Row.RowState <> DataRowState.Detached Then
                If CloneColumns Then
                    If DirectCast(Target.DataSource, DataTable).Columns.Count <> _
                       DirectCast(Source.DataSource, DataTable).Columns.Count Then
                        Target.DataSource = DirectCast(Source.DataSource, DataTable).Clone
                    End If
                End If
            End If
            If IsDataRow(Source) Then
                Dim Position As Integer = Target.Find(KeyField, _
                                          DirectCast(Source.Current, DataRowView).Row(KeyField).ToString)
                If Position = -1 Then
                    DirectCast(Target.DataSource, DataTable).ImportRow(DirectCast(Source.Current, DataRowView).Row)
                Else
                    If Reposition Then
                        Target.Position = Position
                    End If
                End If
            End If
        End Sub
        Private Function IsDataRow(ByVal sender As BindingSource) As Boolean
            If TypeOf sender.Current Is DataRowView Then
                Return True
            Else
                Return False
            End If
        End Function
    End Module
    In form load the code in red would be replaced with your DataTable
    Code:
        Private Sub Form2_Load() Handles MyBase.Load
            '
            ' Load xml data from project resource into a DataTable
            ' which is assigned to the DataSource of the BindingSource
            ' for use as the DataSource for the top DataGridView
            '
            bsCustomers.DataSource = _
            ( _
               From customer In XDocument.Parse(My.Resources.Customers)...<Customer> _
               Select New With _
               { _
                 .Identifier = customer.<CustomerID>.Value, _
                 .CompanyName = customer.<CompanyName>.Value, _
                 .Address = customer.<Address>.Value, _
                 .City = customer.<City>.Value, _
                 .Country = customer.<Country>.Value _
                } _
            ).ToDataTable
            TopGrid.DataSource = bsCustomers
    
            Dim dt As New DataTable
            bsCustomersClone.DataSource = dt.Clone
    
            BottomGrid.DataSource = bsCustomersClone
    
        End Sub
    Since I do not have VS2005 installed at home there may be slight tweaks needed but in the end it will work. Let me know if there is something that is causing issues.

  11. #11

    Thread Starter
    Member
    Join Date
    Jul 2009
    Posts
    56

    Re: Fill dataGridView from DataGridView

    thanks both for the code.

    I think the code from Billboy is the best for my, for now, its more easy to understand
    I want moving forward with this code, if you want.

    for now the code works, but cane you do a update for my?

    When i click one for one, hy is still overwrite the first row.
    Whene i do a multiselectie, then hy place the selected rows, into DataGridView2, thats works good.

    Is it possible, when i make a new column, for datagridView1, white Checkboxen.
    Then when the user do a selectie, into a CheckBox, in DataGridView1, the Row is places into DataGridView2

    thanks,

    Fred
    Holland

    The code
    Code:
    Dim chk As New DataGridViewCheckBoxColumn()
            DataGridView1.Columns.Add(chk)
            chk.HeaderText = "Kruiden selectie"
            chk.Name = "chk"
    
    
    If dt.Rows.Count <= 0 Then
                For i As Integer = 0 To Me.DataGridView1.ColumnCount - 1
                    dt.Columns.Add(Me.DataGridView1.Columns(i).HeaderText, Me.DataGridView1.Columns(i).ValueType)
                Next
                For r As Integer = Me.DataGridView1.SelectedRows.Count - 1 To 0 Step -1
                    Dim dr As DataRow = dt.NewRow()
                    For i As Integer = 0 To Me.DataGridView1.ColumnCount - 1
                        dr.Item(i) = Me.DataGridView1.SelectedRows.Item(r).Cells(i).Value
                    Next
                    dt.Rows.Add(dr)
                Next
            ElseIf dt.Rows.Count >= 1 Then
                For r As Integer = Me.DataGridView1.SelectedRows.Count - 1 To 0 Step -1
                    Dim dr As DataRow = dt.NewRow()
                    For i As Integer = 0 To Me.DataGridView1.ColumnCount - 1
                        dr.Item(i) = Me.DataGridView1.SelectedRows.Item(r).Cells(i).Value
                    Next
                    dt.Rows.Add(dr)
                Next
            End If
            Me.DataGridView2.DataSource = dt

  12. #12
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: Fill dataGridView from DataGridView

    You cant add your CheckBox Column in the same code block you adding rows, thats going to keep adding the checkcolumn

    If you want to add a check box column then add that first, then change you add row code to only add rows that are checked

  13. #13

    Thread Starter
    Member
    Join Date
    Jul 2009
    Posts
    56

    Re: Fill dataGridView from DataGridView

    Quote Originally Posted by billboy View Post
    You cant add your CheckBox Column in the same code block you adding rows, thats going to keep adding the checkcolumn

    If you want to add a check box column then add that first, then change you add row code to only add rows that are checked
    Thank you for react.

    My englisch is not zo very good, but i think i understand you.

    i try to make a new code, that only works for using the checkBox, and not clicking on a row.
    perhaps i must use a button olso after selecting the checkBox?

    maybe you can help me by a new code if you have time

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