Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted

I'd suggest to read this: Linq to datagridview in VB.Net[^]. Author proposes to use extension method, but i think it's possible to achieve in a simpler manner:
VB
'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.
 
Share this answer
 
v2
Comments
sudevsu 15-Apr-15 16:59pm    
Thanks you Maciej. I don't know if you have seen my code above. I have used LINQ. Please read my complete question and explanation. I knew you gave your solution based on the first line
Maciej Los 15-Apr-15 17:04pm    
:)You wrote: how to copy the checked rows into Datagridview2 without the checkboxColumn
I had read your question carefully. As you can see, i use Select New ... With {}. I'd suggest to to re-write columns which you want to add to DGV2, skipping checkBoxColumn.
sudevsu 16-Apr-15 8:51am    
Thank you Macheij. I tried the way you said. But it didn't help.
Here is my code to copy the selected Rows from Datagridview1 to Datagridview2

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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900