将gridview单元格与模板字段合并

时间:2014-10-24 20:17:16

标签: asp.net vb.net gridview

我正在尝试在onrowdatabound事件期间格式化gridview我需要合并具有相同值的列单元格。它的工作原理除了我的一个列之外,它是一个带有链接按钮的模板字段。代码在遍历模板字段列并将整个列合并为单个单元格时读取空白。

标记:

<asp:GridView ID="grdSummary" runat="server" onrowdatabound="grdSummary_RowDataBound" >
                            <Columns>
                                <asp:TemplateField HeaderText="Period" >
                                    <ItemTemplate>
                                        <asp:Linkbutton ID="lnkPeriod" runat="server" CssClass="LinkbuttonCellStyle"
                                            Text='<%# Eval("Period") %>' OnClick="lnkQuarterSummary_Click" CommandArgument='<%# Bind("Period") %>' />
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:BoundField HeaderText="Timeline" DataField="Timeline" />
                                <asp:BoundField HeaderText="Americas" DataField="Americas" DataFormatString="{0:#,0}" ItemStyle-CssClass="SummaryGridCellStyle" />                                    
                            </Columns>
                        </asp:GridView>

代码隐藏:

Protected Sub grdSummary_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

    For rowIndex As Integer = grdSummary.Rows.Count - 2 To 0 Step -1

        Dim grdRow As GridViewRow = grdSummary.Rows(rowIndex)
        Dim grdPreviousRow As GridViewRow = grdSummary.Rows(rowIndex + 1)

        For cellCount As Integer = 0 To 0

            If grdRow.Cells(cellCount).Text = grdPreviousRow.Cells(cellCount).Text Then

                If grdPreviousRow.Cells(cellCount).RowSpan < 2 Then

                    grdRow.Cells(cellCount).RowSpan = 2

                Else

                    grdRow.Cells(cellCount).RowSpan = grdPreviousRow.Cells(cellCount).RowSpan + 1

                End If

                grdPreviousRow.Cells(cellCount).Visible = False

            End If

        Next cellCount

    Next rowIndex

End Sub

我可以使用以下方法读取templatefield值:

Dim test As String = DataBinder.Eval(e.Row.DataItem, "Period")

但我只能读取当前行,而不是前一行。有什么想法吗?

0 个答案:

没有答案