本文介绍了将已检查的行从Datagridview1复制到Datagridview2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Experts,



我有一个带有Checkbox列的datagridview。我想将选中的行复制到Datagridview2中。我能够复制行,但我的代码是复制复选框列的标题文本选择,其值为True。你能否告诉我如何在没有checkboxColumn的情况下将选中的行复制到Datagridview2

Hello Experts,

I have datagridview with a Checkbox column in it. I want to copy the checked rows into Datagridview2. I am able to copy the rows but my code is copying the checkbox column's Header Text Select and its value as True. Could you please tell me how to copy the checked rows into Datagridview2 without the checkboxColumn

DataGridView2.Columns.Clear()
      DataGridView2.DataSource = Nothing
     

      'Copy the selected rows from datagridview 1 
      Dim dt As New DataTable
      For Each col As DataGridViewColumn In dgvShow.Columns
        If Not col.HeaderText.Contains("Select") Then
          dt.Columns.Add(col.HeaderText)
        End If
      Next
      Dim selectedRows As List(Of DataGridViewRow) = (From row In dgvShow.Rows.Cast(Of DataGridViewRow)() Where Convert.ToBoolean(row.Cells("checkBoxColumn").Value) = True).ToList()

      For Each row As DataGridViewRow In selectedRows
        Dim dRow As DataRow = dt.NewRow()
        For Each cell As DataGridViewCell In row.Cells
          If Not cell.Value.ToString().Contains("Select") Then
            dRow(cell.ColumnIndex) = cell.Value
          End If

        Next
        dt.Rows.Add(dRow)
      Next

      DataGridView2.DataSource = dt
      Dim checkBoxColumn As New DataGridViewCheckBoxColumn()
      checkBoxColumn.HeaderText = "Select"
      checkBoxColumn.Width = 40
      checkBoxColumn.Name = "checkBoxColumn"
      DataGridView2.Columns.Insert(0, checkBoxColumn)





请帮助实现上述



Please help in achieving the above

推荐答案

'get selected rows from dgv1 as a list of DataGridViewRow
Dim selectedRows As List(Of DataGridViewRow) = (From row In dgvShow.Rows.Cast(Of DataGridViewRow)() _
    Where row.Cells("checkBoxColumn").Value _
    Select New DataGridViewRow With
    {
       're-write columns, skip checkBoxColumn
    }).ToList()
'add new rows to DataGridView2
DataGridView2.DataSource = selectedRows.Clone()





注意:未经测试!



如果它适用于您,请告诉我。



Note: Not tested!

Please, let me know if it works for you or not.


Try
      Dim selectedRows As List(Of DataGridViewRow) = (From row In dgvShow.Rows.Cast(Of DataGridViewRow)() Where Convert.ToBoolean(row.Cells("checkBoxColumn").Value) = True).ToList()
      If selectedRows.Count > 0 Then
        If DataGridView2.Columns.Count < 0 Then
          For Each c As DataGridViewColumn In dgvShow.Columns
            DataGridView2.Columns.Add(TryCast(c.Clone(), DataGridViewColumn))
          Next
        End If
       

        For Each r As DataGridViewRow In selectedRows
          Dim index As Integer = DataGridView2.Rows.Add(TryCast(r.Clone(), DataGridViewRow))
          For Each o As DataGridViewCell In r.Cells
            DataGridView2.Rows(index).Cells(o.ColumnIndex).Value = o.Value
            r.Cells(0).Value = False
          Next

        Next
      End If
    Catch ex As Exception
      MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
    End Try


这篇关于将已检查的行从Datagridview1复制到Datagridview2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 14:10