如何将整列从一个datagridview复制到另一个?

时间:2021-12-29 15:10:12

如何将整列从一个datagridview复制到另一个?

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

有两个datagridview(dgvReport和dgvReport2)。 dgvReport在选择字段后显示来自服务器的数据(工作正常)。

The checkboxes are 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.

复选框是dgvReport中列的名称。如果用户选择“电子邮件”,例如该列,其“电子邮件”的行数据应添加到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.

我的问题是,当我选择多个复选框时,行的输出仅显示在dgvReport2的第一列,而不是在相应的列下。例如,在屏幕截图中,列“fname”数据显示在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

1 个解决方案

#1


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

#1


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