[英]How to clone and remove unselected rows in DataGridView?
我需要以編程方式刪除DataGridView中除選定行之外的所有行。 首先,我只需要從CtrlDataGridView1中獲取選定的行,並將它們放入dgv中 。 我試過了
Public ReadOnly Property CtrlDataGridView1Filtered() As DataGridView
Get
Dim dgv As New DataGridView
dgv = CtrlDataGridView1
For Each dr As DataGridViewRow In CtrlDataGridView1.Rows
If Not dr.Selected Then
dgv.Rows.Remove(dr)
End If
Next
Return dgv
End Get
End Property
但這不正確,因為CtrlDataGridView1的行已刪除。 但是我只需要刪除dgv的行。
我該怎么辦? 如何解決這個問題?
您可以克隆DataGridView,然后將所需的行復制到其中,例如
'CLONE COLUMNS ONLY'
Dim dgv As New DataGridView
For Each dgvCol As DataGridViewColumn In CtrlDataGridView1.Columns
dgv.Columns.Add(New DataGridViewColumn(dgvCol.CellTemplate))
Next
'COPY SELECTED / UNSELECTED ROWS AS PER YOUR REQUIREMENT'
For Each dr As DataGridViewRow In CtrlDataGridView1.Rows
If dr.Selected Then
'OR If Not dr.Selected Then'
dgv.Rows.Add(dr.Clone)
End If
Next
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.