我创建了一个自定义GridView
(实际上是RadGrid
),其大致如下所示。
在实例化此GridView
并将其添加到我的页面时,我希望第一列在重复的第一行包含“rowspan”属性,但类似的数据。 “rowspan”值应该等于跟随它的类似行的数量。通过这种方式,我希望最终视图更清晰。
我认为,这个逻辑应该在填充行时进行。最初我将行添加到DataTable
,然后将其绑定到GridView
作为最后一步。
这是我尝试的一般逻辑,但它对我不起作用。我做错了什么?
Dim dt As New DataTable() 'Has three default columns
For Each d In Documents 'MAIN ITEM (First Column Data)
Dim rowspan As Integer
rowspan = 0
For Each f In Files
If rowspan = 0 Then
Me.dt.Rows.Add(New Object() {d.Title, f.Language, f.FileSize})
'THIS DOESN'T WORK!
Me.dt.Columns(0).ExtendedProperties.Item("rowspan") = rowspan.ToString()
Else
Me.dt.Rows.Add(New Object() {Nothing, f.Language, f.FileSize})
End If
rowspan += 1
Next
Next
还要记住,这会被转储到按第一列排序的DataView
中,所以我想它必须先实际排序,然后以某种方式计算每个“喜欢”第一列的行数,然后将该列第一行的“rowspan”值设置为等于属于它的行数。
这有意义吗?这是我想在布局中完成的ideal example:
答案 0 :(得分:3)
试试这个。
Protected Sub DemoGrid_PreRender(sender As Object, e As System.EventArgs) Handles DemoGrid.PreRender
MergeRowsWithSameContent(sender)
End Sub
Public Sub MergeRowsWithSameContent(gvw As GridView)
For rowIndex As Integer = gvw.Rows.Count - 2 To 0 Step -1
Dim row As GridViewRow = gvw.Rows(rowIndex)
Dim previousRow As GridViewRow = gvw.Rows(rowIndex + 1)
For i As Integer = 0 To row.Cells.Count - 1
If row.Cells(i).Text = previousRow.Cells(i).Text Then
row.Cells(i).RowSpan = If(previousRow.Cells(i).RowSpan < 2, 2, previousRow.Cells(i).RowSpan + 1)
previousRow.Cells(i).Visible = False
End If
Next
Next
End Sub
P.S:我已使用多年的this wonderful code的无耻端口。