dimanche 3 mai 2015

how to copy whole column from one datagrdview to another in vb.net?

http://ift.tt/1I3tN1c

Please visit the above link to see the screenshot of my program.

There are two datagridview (dgvReport and dgvReport2). dgvReport show a data from server after choosing the fields. (which is working fine).

The checkboxes are the name of columns in dgvReport. If user select "Email" for example the column and it row data of "Email" should be added to dgvReport2.

My problem is when i select more than one checkbox the output of row is shown only at first column of dgvReport2 not under the appropriate column. For example in the screenshot the column "fname" data is showing under email column of dgvReport2.

How can i bring the row data under appropriate column?

Below is my coding:

'Add dynamic column Dim newCol As Integer = 0

        If chkEmail.Checked = True Then
            Dim column As New DataGridViewTextBoxColumn
            dgvReport2.Columns.Insert(newCol, column)

            With column
                .HeaderText = "email"
                .AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
                .ReadOnly = True
            End With

            For rows As Integer = 0 To dgvReport.Rows.Count - 1
                For colcnt As Integer = 0 To dgvReport.Columns.Count - 17
                    dgvReport2.Rows.Add(dgvReport.Rows(rows).Cells(0).Value)
                Next
            Next

            newCol += 1
        End If

        If chkFname.Checked = True Then
            Dim column As New DataGridViewTextBoxColumn
            dgvReport2.Columns.Insert(newCol, column)

            With column
                .HeaderText = "fname"
                .AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
                .ReadOnly = True
            End With

            For rows As Integer = 0 To dgvReport.Rows.Count - 1
                For colcnt As Integer = 0 To dgvReport.Columns.Count - 17
                    dgvReport2.Rows.Add(dgvReport.Rows(rows).Cells(1).Value)
                Next
            Next
            newCol += 1
        End If

Aucun commentaire:

Enregistrer un commentaire