Remove Duplicate Name from gridview. help my code is not working correctly

westdh
westdh used Ask the Experts™
on
Why is this script is removing all the names after the first row for column (cell) 2
Protected Sub uxItemDetailGrid_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles uxItemDetailGrid.RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow Then
        'switch for first row  
        If e.Row.RowIndex = 1 Then
            Dim Gprev As GridViewRow = uxItemDetailGrid.Rows(e.Row.RowIndex - 1)
            If Gprev.Cells(2).Text.Equals(e.Row.Cells(2).Text) Then
                e.Row.Cells(2).Text = ""
            End If
        End If
        'now continue with the rest of the rows 
     
        If e.Row.RowIndex > 1 Then
            'set reference to the row index and the current value 
            Dim intC As Integer = e.Row.RowIndex
            Dim lookfor As String = e.Row.Cells(2).Text
            
            'now loop back through checking previous entries for matches 
            Do
                Dim Gprev As GridViewRow = uxItemDetailGrid.Rows(intC - 1)
                
                If Gprev.Cells(2).Text.Equals(e.Row.Cells(2).Text) Then
                    e.Row.Cells(2).Text = ""
                End If
                intC = intC - 1
            Loop While intC > 0
        End If
    End If
End Sub

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
I think, This Condition is Creating problem
If Gprev.Cells(2).Text.Equals(e.Row.Cells(2).Text) Then


How have you bound ur gridview..

Author

Commented:
Yes ..
I have try this code
===========================================================
Private Values As New ArrayList()

Protected Sub uxItemDetailGrid_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles uxItemDetailGrid.RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim sValue As String = e.Row.Cells(2).Text
       
        If Values.Contains(sValue) Then
            ' This value is a duplicate . So displaying as NULL
            e.Row.Cells(2).Text = " NULL "
        Else
            Values.Add(sValue)
        End If
    End If
End Sub

and I get this ... it is setting everything to NULL after the first row. this is wrong
the duplicate rows are 6 & 7 and 8 & 9

Edit 1  KellcoMacs  xxxxx@kellcomacs.com  25.00  cmp  Ricki Reina  1/12/2010  
Edit 2  NULL  bonnxxxxx@gmail.com  25.00  pnd  Sassy Kellogg  1/19/2010  
Edit 3  NULL  pat.xxxxx@towill.com  25.00  pnd  Pat Christman  1/20/2010  
Edit 4  NULL  anixxxx@earthlink.net  25.00  pnd  Vic Zikoor  1/21/2010  
Edit 5  NULL  cwxxxx@camsys.com  25.00  pnd  Christopher Wornum  1/21/2010  
Edit 6  NULL  xxxxx@sensibleinc.net  50.00  pnd  Robert Sutton  1/22/2010  
Edit 7  NULL  xxxxx@sensibleinc.net  50.00  pnd  Christa Johnson  1/22/2010  
Edit 8  NULL  yyyyyy@pgadesign.com  50.00  pnd  Maura Baldwin  1/22/2010  
Edit 9  NULL  yyyyyy@pgadesign.com  50.00  pnd  Cindy Angers  1/22/2010  
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Top Expert 2013

Commented:
Ok so whats exactly you are trying to do? Did you set breakpoints and see what are the values for your corresponding cell text.
I took your exact row databound code with my sqlDatasource and below is screenshot of what I get. CategoryName is show only once and duplicates are removed. Is that what you want? Then don't see anything wrong in the code as it works as shown in figure. so you problem might be somewhere else.

gvDuplicateCell.bmp
i Check your code. Your logic is right only. please refer attachments
Imports System.Data

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim oDataTable As DataTable
        Dim oDataRow As DataRow

        oDataTable = New DataTable

        oDataTable.Columns.Add("RowNo")
        oDataTable.Columns.Add("Raw_Name")
        oDataTable.Columns.Add("Name")

        For iIndex As Integer = 1 To 20
            oDataRow = oDataTable.NewRow
            oDataRow("RowNo") = iIndex.ToString
            If iIndex Mod 3 = 0 Then
                oDataRow("Raw_Name") = "Name"
                oDataRow("Name") = "Name"
            Else
                oDataRow("Raw_Name") = "Name" & iIndex.ToString
                oDataRow("Name") = "Name" & iIndex.ToString
            End If

            oDataTable.Rows.Add(oDataRow)
        Next

        uxItemDetailGrid.DataSource = oDataTable
        uxItemDetailGrid.DataBind()
    End Sub

    Protected Sub uxItemDetailGrid_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles uxItemDetailGrid.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            'switch for first row   
            If e.Row.RowIndex = 1 Then
                Dim Gprev As GridViewRow = uxItemDetailGrid.Rows(e.Row.RowIndex - 1)
                If Gprev.Cells(2).Text.Equals(e.Row.Cells(2).Text) Then
                    e.Row.Cells(2).Text = ""
                End If
            End If
            'now continue with the rest of the rows  

            If e.Row.RowIndex > 1 Then
                'set reference to the row index and the current value  
                Dim intC As Integer = e.Row.RowIndex
                Dim lookfor As String = e.Row.Cells(2).Text

                'now loop back through checking previous entries for matches  
                Do
                    Dim Gprev As GridViewRow = uxItemDetailGrid.Rows(intC - 1)

                    If Gprev.Cells(2).Text.Equals(e.Row.Cells(2).Text) Then
                        e.Row.Cells(2).Text = ""
                    End If
                    intC = intC - 1
                Loop While intC > 0
            End If
        End If

    End Sub
End Class

Open in new window

Default.aspx.txt
001.JPG
You can try this also
You can try this
Protected Sub uxItemDetailGrid_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles uxItemDetailGrid.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            If e.Row.RowIndex > 0 Then
                'set reference to the row index and the current value  
                Dim intC As Integer = e.Row.RowIndex
                Dim lookfor As String = e.Row.Cells(2).Text

                'now loop back through checking previous entries for matches  
                Do
                    Dim Gprev As GridViewRow = uxItemDetailGrid.Rows(intC - 1)

                    If Gprev.Cells(2).Text.Equals(e.Row.Cells(2).Text) Then
                        e.Row.Cells(2).Text = ""
                    End If
                    intC = intC - 1
                Loop While intC > 0
            End If
        End If

    End Sub

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial