如何将整个列从一个datagridview复制到另一个? [英] How to copy whole column from one datagridview to another?
问题描述
有两个datagridview(dgvReport和dgvReport2).dgvReport选择了这些字段后即可显示来自服务器的数据(正常工作).
There are two datagridview (dgvReport and dgvReport2). dgvReport shows data from server after choosing the fields (which is working fine).
复选框是dgvReport中的列的名称.例如,如果用户选择电子邮件",则应将列及其行数据电子邮件"添加到dgvReport2.
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.
我的问题是,当我选择多个复选框时,仅在dgvReport2的第一列而不是在适当列下显示行的输出.例如,在屏幕截图中,"fname"列数据显示在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?
下面是我的编码:
'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:
替换
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
具有以下内容:
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
这篇关于如何将整个列从一个datagridview复制到另一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!