locked
Need help with eliminating duplicate values from a Gridview with two columns RRS feed

  • Question

  • User126014556 posted

    I have tried to eliminate duplicate values from the first column, but the result displayed only one value in the first column.

    The duplicated row in the first column is the label control id "lblModule", which i am trying to make the duplicates visible to false using on DataBound event.

    ASP page:

    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="default.aspx.vb" Inherits="GICReps._default" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>GIC Reports</title>
        
        <style type="text/css">
            td {
                border-width: thin;
                padding-right: 5px;
                padding-left: 5px;
                padding-bottom: 2px;
                border-top-style: solid;
                border-bottom-style: solid;
            }
            a:link {
                color: #FFFFFF;
                text-decoration: none;
            }
            a:visited {
                color: #FFFFFF;
            }
            a:hover {
                text-decoration: underline;
                color: #FFFFFF;
            }
        </style>
           <meta http-equiv="X-UA-Compatible" content="IE=edge" /> 
    </head>
    <body style="background-image: url('Images/GICLOGO.GIF')">
        <form id="form1" runat="server">
        <div>
        <h3 style="font-family:Verdana; color: #006699; font-size: medium; text-align:center">GIC Reports</h3>
            <br />
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" ShowHeader="False" DataKeyNames="Rep_Code" HorizontalAlign="Center" BorderStyle="None" AllowPaging="True" PageSize="20">
                <Columns>
                    <asp:TemplateField ConvertEmptyStringToNull="False" HeaderText="Module_Name" SortExpression="Module_Name">
                        <ItemTemplate>
                            <asp:Label ID="lblModule" runat="server" Text='<%# Eval("Module_Name") %>' Visible="True"></asp:Label>
                        </ItemTemplate>
                        <ItemStyle VerticalAlign="Top" />
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Url" SortExpression="Url">
                        <ItemTemplate>
                            <asp:LinkButton ID="hLnk" runat="server" NavigateUrl='<%# Eval("Url") %>' Text='<%# Eval("Url") %>' Target="_blank" Visible="true"></asp:LinkButton>
                        </ItemTemplate>
                        <ItemStyle BackColor="#006699" Font-Names="Verdana" Font-Size="small" Wrap="False"/>
                    </asp:TemplateField>
                </Columns>
                <PagerSettings FirstPageText="First&amp;nbsp;" LastPageText="&amp;nbsp;Last" Mode="NextPreviousFirstLast" NextPageText="Next" PreviousPageText="Previous" PageButtonCount="20" />
                <PagerStyle ForeColor="Navy" HorizontalAlign="Center" Wrap="False" />
            </asp:GridView>
            <br />
    
           </div>
            
        </form>
    </body>
    
    </html>

    The code page:

    Imports System.Data.SqlClient
    Imports System.Web.Configuration
    
    Public Class _default
        Inherits System.Web.UI.Page
    
        ' Dim appNames As New List(Of String)
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not Page.IsPostBack Then
                grvBind()
    
            End If
        End Sub
    
    
        Protected Sub GridView1_PageIndexChanging(sender As Object, e As GridViewPageEventArgs) Handles GridView1.PageIndexChanging
    
            GridView1.PageIndex = e.NewPageIndex
            grvBind()
    
    
        End Sub
    
        Protected Sub grvBind()
    
            ' Define the ADO.NET objects.
            Dim connectionString As String =
            WebConfigurationManager.ConnectionStrings("ABE_SystemsCS").ConnectionString
            Dim selectSQL As String =
            "SELECT [NT_GROUP],[Module_Name],[Rep_Code],[Rep_Name],RTRIM([Rep_Web_Link]) As Url FROM [ABE_Systems].[dbo].[VW_Rep_Links] " &
            "WHERE IS_MEMBER ('HQ\' + RTRIM(NT_GROUP)) = 1 ORDER BY [NT_GROUP];"
            Dim con As New SqlConnection(connectionString)
            Dim cmd As New SqlCommand(selectSQL, con)
            Dim adapter As New SqlDataAdapter(cmd)
            ' Fill the DataSet.
            Dim ds As New DataSet()
            adapter.Fill(ds, "[ABE_Systems].[dbo].[VW_Rep_Links]")
            ' Perform the binding.
            GridView1.DataSource = ds
            GridView1.DataBind()
    
        End Sub
    
        Protected Sub GridView1_DataBound(sender As Object, e As EventArgs) Handles GridView1.DataBound
    
            For rowIndex As Integer = GridView1.Rows.Count - 2 To 0 Step -1
                Dim gvRow As GridViewRow = GridView1.Rows(rowIndex)
                Dim gvPreviousRow As GridViewRow = GridView1.Rows(rowIndex + 1)
    
                If gvRow.Cells(0).Text = gvPreviousRow.Cells(0).Text Then
    
                    If gvPreviousRow.Cells(0).RowSpan < 2 Then
                        gvRow.Cells(0).RowSpan = 2
                    Else
                        gvRow.Cells(0).RowSpan = gvPreviousRow.Cells(0).RowSpan + 1
                    End If
    
                    gvPreviousRow.Cells(0).Visible = False
                End If
            Next
    
    
    
        End Sub
    
    End Class

    I appreciate any help

    Tuesday, April 16, 2019 9:36 AM

Answers

  • User126014556 posted

    I have figured it out :) due to template, I will have to use FindControl.

            Dim initialnamevalue As String = TryCast(GridView1.Rows(0).FindControl("lblModule"), Label).Text
    
            For i As Integer = 1 To GridView1.Rows.Count - 1
    
                If TryCast(GridView1.Rows(i).FindControl("lblModule"), Label).Text = initialnamevalue Then
                    TryCast(GridView1.Rows(i).FindControl("lblModule"), Label).Visible = False
                Else
                    initialnamevalue = TryCast(GridView1.Rows(i).FindControl("lblModule"), Label).Text
                End If
            Next
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 16, 2019 12:37 PM