简体   繁体   中英

How to insert specific column of datagridview1 to datagridview2?

Here's a screen shot of my VB.NET Windows Form application.

1个

There are two datagridviews (dgvReport and dgvReport2) in one windows form. dgvReport shows data from server after choosing the fields (which is working fine).

There are checkboxes which are representing the name of columns in dgvReport. If a user selects "Email" for example, the column and its row data of "Email" should be added to dgvReport2.

My problem is that 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, I select two columns "email" and "fname" and then when I click on button "Add" both columns are added in dgvReport2, but column "fname" data showing under email column in dgvReport2 (which is first column).

How can I bring the row data under appropriate column? I thought Index seemed to be good for this purpose but now problem is how to read data from datagridview and assign it in index so that it can be easily inserted into another datagridview?

Below is my code:

'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

I find out the answer of my own question. Hope fully it will help others who are looking for same issue:

replace

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

with the following:

            If dgvReport2.Rows.Count > 0 Then
                For rows As Integer = 0 To dgvReport.Rows.Count - 1
                    dgvReport2.Rows(rows).Cells(newCol).Value =
                    dgvReport.Rows(rows).Cells(1).Value
                Next
            Else
                For rows As Integer = 0 To dgvReport.Rows.Count - 1
                    dgvReport2.Rows.Add(dgvReport.Rows(rows).Cells(1).Value)
                Next
            End If

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM