locked
How can I hide row in a Gridview based on repeated value? RRS feed

  • Question

  • User126014556 posted

    I have the GridView the produces the following rows:

    Column0 Column1 Column2
    Group A Report A Link 1
    Group C Report A Link 1

    Is it possible to hide a row in gridview when a repeated value occurs in Column1? In order to produce the below result:

    Column0 Column1 Column2
    Group A Report A Link 1

     Code page:

    Imports System.Data.SqlClient
    Imports System.Web.Configuration
    
    Public Class _default
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
        End Sub
    
        Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
    
    
            Dim hLink As HyperLink = TryCast(e.Row.FindControl("hLnk"), HyperLink)
            Dim lblApp As Label = TryCast(e.Row.FindControl("lblAppName"), Label)
            Dim lblGrp As Label = TryCast(e.Row.FindControl("lblGroup"), Label)
            Dim lblCod As Label = TryCast(e.Row.FindControl("lblCode"), Label)
    
            Try
    
                If e.Row.RowType = DataControlRowType.DataRow Then
    
                    e.Row.Visible = IsInGroup(lblGrp.Text.Trim())
    
                End If
    
            Catch ex As Exception
    
            End Try
    
        End Sub
    
        Public Function IsInGroup(ByVal GroupName As String) As Boolean
    
            Dim MyIdentity As System.Security.Principal.WindowsIdentity = System.Security.Principal.WindowsIdentity.GetCurrent()
            Dim MyPrincipal As System.Security.Principal.WindowsPrincipal = New System.Security.Principal.WindowsPrincipal(MyIdentity)
            Return MyPrincipal.IsInRole(GroupName)
    
        End Function
    
    End Class

    I apprciate any help, thanks in advance.

    Wednesday, April 18, 2018 7:23 PM

Answers

  • User-1716253493 posted
            If e.Row.RowType = DataControlRowType.DataRow Then
                Dim hLink As HyperLink = TryCast(e.Row.FindControl("hLnk"), HyperLink)
                Dim lblApp As Label = TryCast(e.Row.FindControl("lblAppName"), Label)
                Dim lblGrp As Label = TryCast(e.Row.FindControl("lblGroup"), Label)
                Dim lblCod As Label = TryCast(e.Row.FindControl("lblCode"), Label)
                If Not ViewState("col1") = Nothing Then
                    If ViewState("col1") = lblGrp.Text Then
                        e.Row.Visible = False
                    Else
                        ViewState("col1") = lblGrp.Text
                    End If
                Else
                    ViewState("col1") = lblGrp.Text
                End If
            End If

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 18, 2018 8:11 PM