Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, i accidentally deleted the previous question but my problem istn solved yet so....
I had some great help on your site, found the solution to some stuff i was struggling with, but now i got a question myself:
I used the code from this page: link [^]
and it works great. it adds new coloms the the 2nd datagrid and fills them. However, Is it possible to have it add the selected data into new rows in excisting columns? and then so that it wil automatically create a new row if row 1 and 2(for example) are already populated(or is this not possible? )
That way i can fill a datagridview with data from 2 other datagridviews which will save me quite some time.

Update
Per request, the code i already tried. It works if you want to fill a empty dgv, but i want to fill a allready populated dgv.
VB
Private Sub btncopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncopy.Click
        Dim c, t As Integer
        'dg2.Columns.Clear()

        For t = 0 To DataGridView1.Columns.Count - 1
            DGV2.Columns.Add(DataGridView1.Columns(t).Clone())
        Next
 
        For c = 0 To DataGridView1.Rows.Count - 1
 
            If DataGridView1.Rows(c).Cells(4).Value = True Then
                DGV2.Rows.Add(DataGridView1.Rows(c).Cells(0).Value, DataGridView1.Rows(c).Cells(1).Value)
                DGV2.Columns(2).Visible = False
                DGV2.Columns(3).Visible = False
 
            End If
 
        Next
    End Sub

And after some comments i got this code.
VB
For i As Integer = 0 To Rantsoengrid.Rows.Count - 1
        If Rantsoengrid.Rows(i).Cells(3).Value = True Then
            DGV2.Rows.Add(Rantsoengrid.Rows(i).Cells(3).Value, _
            Rantsoengrid.Rows(i).Cells(2).Value, _
            Rantsoengrid.Rows(i).Cells(4).Value, _
            Rantsoengrid.Rows(i).Cells(6).Value)

        End If
    Next

End Sub

update
Okay, stupid me. referenced to the wrong datagridview in the code.
Now all that remains is converting the data from a string to boolean. (of which i know its true or false)i wil ltry to figure that one out myself but ofcourse, if you know the answer, feel free to post it for future reference (for other people)
De conversie van tekenreeks  naar type Boolean is ongeldig.


And, if i use the cells from the original datagridvieuw(0,1,2,3) instead of the ones i want to copy the data to (2,3,4,6) then i get the error that the programm cant add rows because the datagridview is databound.(removing the binding is not a option, due to the other data in the datagridview)

Okay, update because i did solve some things now, but not this one.
I think the way we need to work from is the error
"Invalid Operationexception was unhandled"
This is because the errorreport states that it cannot copy the data to a databound datagridview. Is there any way around this?
Posted
Updated 1-Oct-14 1:46am
v5
Comments
Tino Fourie 26-Sep-14 10:23am    
CBool

1 solution

I posted the 2nd code block from your previous, now deleted post. You will not solve the various problems you are experiencing with DataGridViews until you understand how they work and what you need to make them work.

First you asked to add rows to an already populated datagrid. I gave you the code. You take my code and copy it exactly and yet now you getting an error you did not previously had.

I strongly suggest two things: (1) Learn more about DataGridView and (2) Step through your code and check to see what happens and when it happens.

You have two possible indexes: Rows and Columns indexes. The code for the Rows index is perfectly correct, I wrote that myself. I can not say the same for your Columns since you adapted the code.

My gut feel says you do not have 7 columns in your datagrid !!! (I might be wrong however I strongly doubt that)

I am going to say it again. Both Rows and Columns are 0 based. This means you start your count at 0 (not 1).
 
Share this answer
 
Comments
Member 11088476 25-Sep-14 1:51am    
Hello,
First of all, thanks.
I already use the DGV way of counting(of which i already knew to start with 0 for the first column) i got 7 colums. So that should be fine too. I get the error on this line: If Rantsoengrid.Rows(i).Cells(4).Value = True Then

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900