Reputation: 988
Using Windows form (Vb.net & Datagridview)
Checkbox is using in datagridview row. I want to copy the selected row to new row.
For Example
ID Value Checkbox
001 100 Checked
002 200 Checked
003 250 Unchecked
Then i try to copy, then it should show as
ID Value Checkbox
001 100 Checked
002 200 Checked
003 250 Unchecked
001 100 Checked
002 200 Checked
Tried Code.
For i As Integer = grvList.RowCount - 1 To 0 Step -1
If grvList.Rows(i).Cells(0).Value = True Then
For j As Integer = 0 To grvList.Rows(i).Cells.Count - 1
MsgBox(grvList.Rows(i).Cells(j).Value)
Next
End If
Next
'
grvlist - Datagridview
cells(0) - checkbox used
The above code is working for if checkbox checked then it is taking the current row value. But i want to add new row, to copy all the current row values. How to do it.
Need Vb.net code help
Upvotes: 0
Views: 2902
Reputation: 2595
DataGridView has a built in copy function that should work.
EDIT: AddCopy doesn't work as advertised, it inserts an empty row with the same style as the source row. We have to manually copy the cell data over to the new row. Updated code below.
For i As Integer = grvList.RowCount - 1 To 0 Step -1
If grvList.Rows(i).Cells(0).Value = True Then
Dim NewRowIndex as integer = grvList.Rows.AddCopy(i)
For j = 0 To (grvList.ColumnCount - 1)
grvList.Rows(NewRowIndex).Cells(j).value = _
grvList.Rows(i).Cells(j).value
Next
End If
Next
Upvotes: 1