开发者

How to get previous row's specific value to compare with next row value in a gridview

开发者 https://www.devze.com 2023-01-22 10:20 出处:网络
I have a GridView which binds data from DB, and using rowboundevent, I want to get the second cell value of each row and compare it with the second cell value of next row. If they are the same then th

I have a GridView which binds data from DB, and using rowboundevent, I want to get the second cell value of each row and compare it with the second cell value of next row. If they are the same then the cell should be empty, else the value should be written in the cell. 开发者_如何学运维


This should work(you didn't mention what language), if you actually want all but the last of the equal rows with empty text:

 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

Tested with this Pseudo-Data in 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


On your RowBoundEvent store the previous value in session. You'd then compare the current item to the value in Session.

Let me know if you know more detail.

0

精彩评论

暂无评论...
验证码 换一张
取 消