I tried to insert the column name from datagridview1 to rows of datagridview2 and that was work, but the first row problem. when moving the mouse to the end position and back to the first position, the value does not appear.
Thanks for your help

Private Sub DataGridView1_CellMouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseClick
Try
For srow As Integer = 0 To DataGridView1.Rows.Count - 1
If DataGridView2.Rows.Count > 1 And DataGridView2.Rows(0).Cells(0).Value = DataGridView1.Columns(0).HeaderText Then
DataGridView2.Rows.Clear()
ElseIf DataGridView1.Rows(srow).Selected Then
DataGridView1.Rows(srow).Selected = True
Dim col1 As String = DataGridView1.Columns(0).HeaderText
Dim row1 As String = DataGridView1.Rows(srow).Cells(0).Value
DataGridView2.Rows.Add(col1, row1)
Dim col2 As String = DataGridView1.Columns(1).HeaderText
Dim row2 As String = DataGridView1.Rows(srow).Cells(1).Value
DataGridView2.Rows.Add(col2, row2)
Dim col3 As String = DataGridView1.Columns(2).HeaderText
Dim row3 As String = DataGridView1.Rows(srow).Cells(2).Value
DataGridView2.Rows.Add(col3, row3)
Dim col4 As String = DataGridView1.Columns(3).HeaderText
Dim row4 As String = DataGridView1.Rows(srow).Cells(3).Value
DataGridView2.Rows.Add(col4, row4)
Return
End If
Next
Catch ex As Exception
End Try
End Sub

Posted Sep 30, 2013, 10:24 AM
I modified your code little bit.
Always clear the second grid and handled dbnull exception also.
Sub DataGridView1_MouseClick(sender As Object, e As MouseEventArgs)
Try
For srow As Integer = 0 To DataGridView1.Rows.Count - 1
DataGridView2.Rows.Clear()
If DataGridView1.Rows(srow).Selected Then
DataGridView1.Rows(srow).Selected = True
Dim col1 As String = DataGridView1.Columns(0).HeaderText
Dim row1 As String
If IsDBNull(DataGridView1.Rows(srow).Cells(0).Value) = False Then
row1 = DataGridView1.Rows(srow).Cells(0).Value
End If
DataGridView2.Rows.Add(col1, row1)
Dim col2 As String = DataGridView1.Columns(1).HeaderText
Dim row2 As String
If IsDBNull(DataGridView1.Rows(srow).Cells(1).Value) = False Then
row2 = DataGridView1.Rows(srow).Cells(1).Value
End If
DataGridView2.Rows.Add(col2, row2)
Dim col3 As String = DataGridView1.Columns(2).HeaderText
Dim row3 As String
If IsDBNull(DataGridView1.Rows(srow).Cells(2).Value) = False Then
row3 = DataGridView1.Rows(srow).Cells(2).Value
End If
DataGridView2.Rows.Add(col3, row3)
Dim col4 As String = DataGridView1.Columns(3).HeaderText
Dim row4 As String
If IsDBNull(DataGridView1.Rows(srow).Cells(3).Value) = False Then
row4 = DataGridView1.Rows(srow).Cells(3).Value
End If
DataGridView2.Rows.Add(col4, row4)
Return
End If
Next
Catch ex As Exception
MessageBox.Show (ex.Message)
End Try
End Sub
Hope this helps you.
Posted Sep 30, 2013, 11:21 AM
Please mark as answer so that others will get helped.
fugio huynhPosted Sep 30, 2013, 11:14 AM