Hi,

I have to forms, second for is shown from the first form trough showdialog command. Both forms have datagridview
User will select data from DGV in second form then pass all this data to DGV in first form

How can I achieve it, I know how to do it with single data only, how if its multiple data?
please help

Thanks

You can did it by a for loop
like

For Each r As DataGridViewRow In DataGridView1.SelectedRows
' Process your Data
        Next

I did something like that but there's an error.
cannot add row to the DGV of first form because it has no columns, but my DGV has.

I also tried creating a function in the second form that retuns a value to the first form after it has been disposed. But thats only for single data. What if in my case, the DGV in second form has lots of rows (data). Do i need to create array or something?

thanks again

Dynamically add column to the DGV in First Form, then transfer data.
You already coded to transfer data by a loop on DataGridView SelectedRows, so no need to hold data in an array.

I already did that, dynamically and manually adding columns in form 1

when transferring multiple data from second form to first form, error occurs saying no column is added in DGV in first form when in fact i already have added manually

I also tried adding columns in DGV (form 1) from my second form but no rows were added

by the way, my second form is modal

Please post your codes and exception.

Form 1

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
    Form2.Showdialog
End Sub

Form 2

Selecting of Data in Datagridview. Say dgvSelected has 5 rows
This 5 rows of Data should be sent back to Form 1

Private Sub btnDone_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDone.Click
    For Each row As DataGridViewRow In dgvSelect.Rows
                    Form1.dgFinal.Rows.Add(row.Cells(0).Value, row.Cells(1).Value)
    Next
End Sub

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

I added columns in dgFinal (Form 1), no error, no Data also

Your loop should be like

 DataGridView2.Rows.Clear()
 For Each r As DataGridViewRow In DataGridView1.Rows
    Dim x As String() = {r.Cells(0).Value, r.Cells(1).Value}
    DataGridView2.Rows.Add(x)
Next

I tried your solution but no record is transfered to datagridview in form1
Form 2 after pressing done is diposed

Private Sub btnDone_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDone.Click

Dim i As Integer = dgv1.CurrentRow.Index

    form2.dgv2.Rows.Add(dgv1.Item(0, i).Value, dgv1.Item(1, i).Value, dgv1.Item(2, i).Value, dgv1.Item(3, i).Value, dgv1.Item(5, i).Value,dgv1.Item(5, i).Value)
    form2.show()

end sub

thanks for the reply but I guess you did'nt get the problem

to clarify agen, I have 2 forms, Form 2 is created from Form 1 through showdialog

I need to select data from datagridview in form 2
then when button done is click, selected data from form 2 should be transfered to datagridview of form 1 then dispose form 2, (data transfered has 2 columns)

to make it short, Ive created form 2 for selection of data

hope it's clear

please help

thanks

@Lethugs: I surprise, why do not the codes work? In my system it works.

Here I took two forms, Form1 and Form2. Form2 is main form and Form1 is the modal form, which displays by clicking the Button on form2.
Here the codes for ButtonShowForm1 are

Private Sub ButtonShowForm1_Click(sender As System.Object, e As System.EventArgs) Handles ButtonShowForm1.Click
        Form1.ShowDialog()
End Sub

The codes for ButtonShowResult are

Private Sub ButtonShowResult_Click(sender As System.Object, e As System.EventArgs) Handles ButtonShowResult.Click
        Form2.DataGridView1.Rows.Clear()

        For Each r As DataGridViewRow In Me.DataGridView1.Rows
            Dim x As String() = {r.Cells(0).Value, r.Cells(1).Value}
            Form2.DataGridView1.Rows.Add(x)
        Next

    End Sub

The resulted picture are below 5ce7c2c8a8f09ec49da1a0c114eb171a
And cf92858039cdc5a3b98d5d8341b7a977

Hi

I tried again your solution, same problem occurs. Cannot add rows if no column is added. I dont know what the cause of it. I even remove all my codes except for code related to datagrid just to make sure nothing affects its behaviour

I ended up using datatable, function that returns a datatable from the modal form then set datagridview of first form datasource to that datatable

thank you for helping me. Hope I can still do your code

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.