fugio huynh

fugio huynh

  • NA
  • 51
  • 16.8k

add column headertext datagridview1 to rows datagridview2

Sep 30 2013 6:30 AM
Hello Everyone,
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


Answers (3)