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.
views:
47answers:
2
A:
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.
Esteban Araya
2010-10-27 17:25:27
should i use a hidden field to store the value
Srivastava
2010-10-27 17:27:56
Why go all the way to the session to store a temporary variable, would seem just declaring a variable would work just as well here . . .
Wyatt Barnett
2010-10-27 18:50:06
@Wyatt: Because when the form re-renders, he may want to know what the last value of the last row was.
Esteban Araya
2010-10-28 17:29:28
+1
A:
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
Tim Schmelter
2010-10-27 18:25:55