如何获取上一行的特定值以与gridview中的下一行值进行比较

时间:2010-10-27 17:18:18

标签: asp.net sql-server

我有一个GridView,它绑定来自DB的数据,并使用rowboundevent,我想获取每一行的第二个单元格值,并将其与下一行的第二个单元格值进行比较。如果它们相同则单元格应为空,否则应将值写入单元格中。

2 个答案:

答案 0 :(得分:1)

这应该有效(你没有提到什么语言),如果你真的想要除了最后一行的空文本之外的所有文字:

 Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
    Select Case e.Row.RowType
        Case DataControlRowType.DataRow
            Dim thisRow As DataRowView = DirectCast(e.Row.DataItem, DataRowView)
            Dim nextRow As DataRowView
            Select Case e.Row.DataItemIndex
                Case Is <> thisRow.DataView.Count - 1
                    nextRow = thisRow.DataView(e.Row.DataItemIndex + 1)
            End Select
            e.Row.Cells(0).Text = thisRow(0).ToString
            If Not nextRow Is Nothing AndAlso nextRow(1).Equals(thisRow(1)) Then
                'for testing purposes i did not take String.Empty but the Number at the end'
                e.Row.Cells(1).Text = thisRow(1).ToString.Split(" "c)(1)
            Else
                e.Row.Cells(1).Text = thisRow(1).ToString
            End If
    End Select
End Sub

在Page.Load:

中使用此伪数据进行测试
If Not IsPostBack Then
    Dim tbl As New DataTable
    Dim rnd As New Random()
    tbl.Columns.Add(New DataColumn("ID", GetType(Int32)))
    tbl.Columns.Add(New DataColumn("TEXT", GetType(String)))
    For i As Int32 = 1 To 20
        Dim id As Int32 = i
        Dim text As String = "Text " & rnd.Next(1, i)
        Dim row As DataRow = tbl.NewRow
        row("ID") = id
        row("TEXT") = text
        tbl.Rows.Add(row)
    Next
    Dim view As DataView = tbl.DefaultView
    Me.GridView1.DataSource = view
    Me.GridView1.AutoGenerateColumns = True
    Me.GridView1.DataBind()
End If

答案 1 :(得分:-1)

RowBoundEvent商店中,会话中的上一个值。然后,您将当前项目与会话中的值进行比较。

如果您了解更多细节,请与我们联系。