我的datagridview出现问题。如果column2和column3相同,我该如何合并行呢?
例如:
Col 1| Col2| Col3|
2| Mars|Regular
3| Mars|Regular
如何将其合并为一列?
答案 0 :(得分:0)
这样的事情应该回答你的问题:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
' Populate manually your datagridview
Dim row As Object() = New Object() {2, "Mars", "Regular"}
dtgv.Rows.Add(row)
row = New Object() {3, "Mars", "Regular"}
dtgv.Rows.Add(row)
row = New Object() {10, "Earth", "Regular"}
dtgv.Rows.Add(row)
' Try to merge rows
For i = 0 To dtgv.RowCount - 1
merge(i)
Next
End Sub
Public Sub merge(ByVal row As Integer)
Dim planet As Boolean = False
Dim regularity As Boolean = False
' If planets are identicals
If dtgv.Rows(row).Cells(1).Value = dtgv.Rows(row + 1).Cells(1).Value Then
planet = True
End If
' Same
If dtgv.Rows(row).Cells(2).Value = dtgv.Rows(row + 1).Cells(2).Value Then
regularity = True
End If
' Merge
If planet = True And regularity = True Then
' Sum up index
dtgv.Rows(row).Cells(0).Value = dtgv.Rows(row).Cells(0).Value + dtgv.Rows(row + 1).Cells(0).Value
' Remove one row
dtgv.Rows.RemoveAt(row + 1)
' Message to warn there is a merge
MsgBox("Rows " & row & " and " & row + 1 & " merged !")
End If
End Sub
只有两个第一行匹配,因为第三行具有不同的行星。