PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2010 [RESOLVED] How to add selected rows from Form2 DGV to Form1 Data Bound DGV-VBForums
Results 1 to 7 of 7

Thread: [RESOLVED] How to add selected rows from Form2 DGV to Form1 Data Bound DGV

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2011
    Posts
    76

    Resolved [RESOLVED] How to add selected rows from Form2 DGV to Form1 Data Bound DGV

    Dear All,

    I am struck with an issue when adding selected rows from form2 DGV to form1 DGV which is already binded with access DB rows (data) and its throwing error "Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound.", and i search through this vbforums and get an idea that we can do this with the help of Data Table but i don't know how to implement this with my existing codes.
    please go through my codes below.

    Form1 DGV which is binded with the below code
    Code:
    'In form Load
    Dim Con = New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "\Data.mdb;" & _
                                                "Jet OLEDB:Database Password=12345;")
            Con.Open()
            With Me.DataGridView1
                Dim cmd As OleDbCommand = New OleDbCommand("SELECT ProjectResources.ResourceID, ProjectResources.Name FROM ProjectResources WHERE ProjectResources.ProjectID = " & TextBox1.Text & " ", Con)
                Dim myDA As OleDbDataAdapter = New OleDbDataAdapter(cmd)
                Dim myDataSet As DataSet = New DataSet()
                Me.DataGridView1.AutoGenerateColumns = False
                myDA.Fill(myDataSet, "M1")
                Me.DataGridView1.DataSource = myDataSet.Tables("M1").DefaultView
            End With
            Con.Close()
    Form2 DGV which is binded with the below code
    Code:
    'In form Load
    Dim Con = New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "\Data.mdb;" & _
                                                "Jet OLEDB:Database Password=12345;")
            Con.Open()
            With Me.DataGridView1
                Dim cmd As OleDbCommand = New OleDbCommand("SELECT ResourceMaster.ResourceID,ResourceMaster.Name, ResourceMaster.IsResource FROM ResourceMaster WHERE ResourceMaster.IsResource = 1", Con)
                Dim myDA As OleDbDataAdapter = New OleDbDataAdapter(cmd)
                Dim myDataSet As DataSet = New DataSet()
                Me.DataGridView1.AutoGenerateColumns = False
                myDA.Fill(myDataSet, "M2")
                Me.DataGridView1.DataSource = myDataSet.Tables("M2").DefaultView
            End With
            Con.Close()
    In Form2 DGV if user selects the DGV chk box and give Ok then the selected rows will show in form1 DGV
    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim IsSelected As Boolean
            Dim i As Int16
            Dim RCode As String
            Dim ResourceName As String
            Dim Unit As String
            With Me.DataGridView1
                For i = 0 To .RowCount - 1
                    RCode = .Rows(i).Cells(0).Value
                    ResourceName = .Rows(i).Cells(1).Value
                    Unit = .Rows(i).Cells(2).Value
                    IsSelected = .Rows(i).Cells(3).Value
                    If IsSelected = True Then
                        With frmOHResource.DataGridView1
                            .Rows.Add(1)
                            .Rows(.RowCount - 1).Cells(0).Value = RCode
                            .Rows(.RowCount - 1).Cells(1).Value = ResourceName
                            .Rows(.RowCount - 1).Cells(2).Value = Unit
                        End With
                    End If
                Next
            End With
        End Sub
    Thanks...

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,242

    Re: How to add selected rows from Form2 DGV to Form1 Data Bound DGV

    You must add a new datarow to the datatable which is the DataSource for the target DGV using the values from the source DGV. When the new row is added to the table, the DGV should automatically update via the binding.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2011
    Posts
    76

    Re: How to add selected rows from Form2 DGV to Form1 Data Bound DGV

    Quote Originally Posted by dunfiddlin View Post
    You must add a new datarow to the datatable which is the DataSource for the target DGV using the values from the source DGV. When the new row is added to the table, the DGV should automatically update via the binding.
    My targeted DGV data table is myDataSet for your reference see below code
    Code:
    Dim Con = New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "\Data.mdb;" & _
                                                "Jet OLEDB:Database Password=12345;")
            Con.Open()
            With Me.DataGridView1
                Dim cmd As OleDbCommand = New OleDbCommand("SELECT ProjectResources.ResourceID, ProjectResources.Name FROM ProjectResources WHERE ProjectResources.ProjectID = " & TextBox1.Text & " ", Con)
                Dim myDA As OleDbDataAdapter = New OleDbDataAdapter(cmd)
                Dim myDataSet As DataSet = New DataSet()
                Me.DataGridView1.AutoGenerateColumns = False
                myDA.Fill(myDataSet, "M1")
                Me.DataGridView1.DataSource = myDataSet.Tables("M1").DefaultView
            End With
            Con.Close()
    As per my understanding I need to create or insert the targeted DGV data table to the below code somewhere else (really i don't know how to start from the below code
    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim IsSelected As Boolean
            Dim i As Int16
            Dim RCode As String
            Dim ResourceName As String
            Dim Unit As String
            With Me.DataGridView1
                For i = 0 To .RowCount - 1
                    RCode = .Rows(i).Cells(0).Value
                    ResourceName = .Rows(i).Cells(1).Value
                    Unit = .Rows(i).Cells(2).Value
                    IsSelected = .Rows(i).Cells(3).Value
                    If IsSelected = True Then
                        With frmOHResource.DataGridView1
    
    ' I think here myDataSet to be added
    
                            .Rows.Add(1)
                            .Rows(.RowCount - 1).Cells(0).Value = RCode
                            .Rows(.RowCount - 1).Cells(1).Value = ResourceName
                            .Rows(.RowCount - 1).Cells(2).Value = Unit
                        End With
                    End If
                Next
            End With
        End Sub

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    103,238

    Re: How to add selected rows from Form2 DGV to Form1 Data Bound DGV

    What is the relationship between the forms? The data is being passed from Form2 to Form1, right? Is Form1 opening Form2? Is Form2 closing when these rows are added or is it staying open for further operations?

    Also, do the two DataTables have the same schema?

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Oct 2011
    Posts
    76

    Re: How to add selected rows from Form2 DGV to Form1 Data Bound DGV

    Quote Originally Posted by jmcilhinney View Post
    What is the relationship between the forms? The data is being passed from Form2 to Form1, right? Is Form1 opening Form2? Is Form2 closing when these rows are added or is it staying open for further operations?

    Also, do the two DataTables have the same schema?
    Thanks for your reply jmc..
    Yes The data is being passed from Form2 to Form1, Form1 contains a button which opens the Form2 and in the Form2 there is a DGV with chk box column so users can select the required rows and click Ok button in Form2 which pass the Form2 selected rows to From1 DGV and the Form2 will close.
    My Code works when the Form1 DGV is not binded with any data, but when i bind the data to Form1 DGV on form load, Form2 selected rows is not getting passed and its throwing error (please refer thread #1).
    Please guide me.

  6. #6
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,242

    Re: How to add selected rows from Form2 DGV to Form1 Data Bound DGV

    vb.net Code:
    1. ' Simple example
    2.  
    3. ' Form2.dgv has additional last column as checkbox
    4.  
    5. Public Class Form1
    6.     Public dt As New DataTable
    7.     Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    8.         dt.Columns.Add("A")
    9.         dt.Columns.Add("B")
    10.         dt.Columns.Add("C")
    11.         dgv.DataSource = dt
    12.         Form2.ShowDialog()
    13.     End Sub
    14. End Class
    15.  
    16. Public Class Form2
    17.  
    18.     Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    19.  
    20.         For Each row As DataGridViewRow In Me.dgv.Rows ' loop through all rows of Form2.dgv
    21.  
    22.             If CBool(row.Cells(3).Value) Then ' if checked
    23.                 Dim r = Form1.dt.NewRow ' create new datarow with schema from dt
    24.                 With row
    25.                     r.ItemArray = {.Cells(0).Value, .Cells(1).Value, .Cells(2).Value} ' fill new row with values
    26.                 End With
    27.                 Form1.dt.Rows.Add(r) ' add new row to datatable
    28.  
    29. ' Form1.dgv now updates automatically through the data binding
    30.  
    31.             End If
    32.         Next
    33.  
    34.         Me.Close()
    35.  
    36.     End Sub
    37. End Class
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Oct 2011
    Posts
    76

    Re: How to add selected rows from Form2 DGV to Form1 Data Bound DGV

    Quote Originally Posted by dunfiddlin View Post
    vb.net Code:
    1. ' Simple example
    2.  
    3. ' Form2.dgv has additional last column as checkbox
    4.  
    5. Public Class Form1
    6.     Public dt As New DataTable
    7.     Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    8.         dt.Columns.Add("A")
    9.         dt.Columns.Add("B")
    10.         dt.Columns.Add("C")
    11.         dgv.DataSource = dt
    12.         Form2.ShowDialog()
    13.     End Sub
    14. End Class
    15.  
    16. Public Class Form2
    17.  
    18.     Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    19.  
    20.         For Each row As DataGridViewRow In Me.dgv.Rows ' loop through all rows of Form2.dgv
    21.  
    22.             If CBool(row.Cells(3).Value) Then ' if checked
    23.                 Dim r = Form1.dt.NewRow ' create new datarow with schema from dt
    24.                 With row
    25.                     r.ItemArray = {.Cells(0).Value, .Cells(1).Value, .Cells(2).Value} ' fill new row with values
    26.                 End With
    27.                 Form1.dt.Rows.Add(r) ' add new row to datatable
    28.  
    29. ' Form1.dgv now updates automatically through the data binding
    30.  
    31.             End If
    32.         Next
    33.  
    34.         Me.Close()
    35.  
    36.     End Sub
    37. End Class
    Thank you All... Problem solved

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width