Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27005

VS 2010 How to add selected rows from Form2 DGV to Form1 Data Bound DGV

$
0
0
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...

Viewing all articles
Browse latest Browse all 27005

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>