DataGridView Question contents of two columns

Joe1

Active member
Joined
Dec 4, 2012
Messages
37
Programming Experience
Beginner
Hello,

I have two windows operating DataGridViews and am trying to add the contents of two columns from the first window into the second window. (only for the rows whose checkbox is selected)

I am currently able to copy either one of the two columns, but am struggling to get both at the same time, using this code below:


In the first window:

For i As Integer = 0 To DataGridView1.Rows.Count - 1
Dim row As DataGridViewRow = DataGridView1.Rows(i)
If row.Cells("DataGridViewCheckBoxColumn1").Value = True Then
frm.AddItem(row.Cells(1).Value)
If row.Cells("DataGridViewTextBoxColumn3").Value.ToString = "" Then
MsgBox("Please select a 'User Reference' for all selected parameters.")
Exit Sub
End If
End If

And then in the second window:

Public Sub AddItem(ByVal UserReference As Object)
DataGridView1.Rows.Add(UserReference, "This needs to query value", "Parameter Name")
End Sub

In this I have moved the User Reference Column - row.Cells(1)
but I am also trying to move the parameter column - which is row.Cells(2)

in the 3rd column in second window in the place of the constant "Parameter Name"
 
Hi,

The easy way to do this is to pass the actual DataGridViewRow as the parameter to your AddItem routine in the second form and then do whatever necessary checks you need to before the row is added to the second DataGridView. See Here:-

Form 1:-
VB.NET:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
  For Each row As DataGridViewRow In DataGridView1.Rows
    If CBool(row.Cells("Column1").Value) = True Then
      If Not IsNothing(row.Cells("Column4").Value) Then
        Form2.AddItem(row)
      Else
        MsgBox("Please select a 'User Reference' for all selected parameters.")
        Exit For
      End If
    End If
  Next
End Sub

Form 2:-
VB.NET:
Public Sub AddItem(ByVal UserReference As DataGridViewRow)
  DataGridView1.Rows.Add(UserReference.Cells(1).Value, "This needs to query value", UserReference.Cells(2).Value)
End Sub

Hope that helps,

Cheers,

Ian
 
Back
Top