Proble with Custom property used to remove duplicates
-
Hi all, I have a Gridview with with 4 columns....Brand,Category,Item,Stock The user wants that the for all items belonging to same Brand and category, the Brand and Category needs to be shown only once. For this i wrote the following code but it doesn`t seems to work...
Public Class GridItem
Dim _brand As String
Dim _cat As StringPublic Sub GridItem() 'Constructor \_brand = "" \_cat = "" End Sub Public Property Brand() As String Get Return \_brand End Get Set(ByVal value As String) \_brand = value End Set End Property Public Property Category() As String Get Return \_cat End Get Set(ByVal value As String) \_cat = value End Set End Property
End Class
Sub ItemGrid_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then Dim GI As New GridItem GI.Brand = e.Row.Cells(0).Text GI.Category = e.Row.Cells(1).Text If uniquearr.Contains(GI) Then e.Row.Cells(0).Text = "" e.Row.Cells(1).Text = "" Else uniquearr.Add(GI) End If End If End Sub
uniquearr is declared global.Any help would be appreciated
-
Hi all, I have a Gridview with with 4 columns....Brand,Category,Item,Stock The user wants that the for all items belonging to same Brand and category, the Brand and Category needs to be shown only once. For this i wrote the following code but it doesn`t seems to work...
Public Class GridItem
Dim _brand As String
Dim _cat As StringPublic Sub GridItem() 'Constructor \_brand = "" \_cat = "" End Sub Public Property Brand() As String Get Return \_brand End Get Set(ByVal value As String) \_brand = value End Set End Property Public Property Category() As String Get Return \_cat End Get Set(ByVal value As String) \_cat = value End Set End Property
End Class
Sub ItemGrid_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then Dim GI As New GridItem GI.Brand = e.Row.Cells(0).Text GI.Category = e.Row.Cells(1).Text If uniquearr.Contains(GI) Then e.Row.Cells(0).Text = "" e.Row.Cells(1).Text = "" Else uniquearr.Add(GI) End If End If End Sub
uniquearr is declared global.Any help would be appreciated
I will answer in C#, if you don't understand tell me. Your problem is the following: (1) Contains uses Equals. (2)
GridItem g1 = new GridItem("bla", "bla");
GridItem g2 = new GridItem("bla", "bla");if (g1.Equals(g2))
Response.Write("equal");
else
Response.Write("not equal");This returns not equal. Why? If the GridItem does not implement an Equals function, it will check the equality of the HashCode.
Response.Write(g1.GetHashCode().ToString());
Response.Write(g2.GetHashCode().ToString());Two different hashcodes. So how do we solve this? Add an Equals function to your GridItem:
public override bool Equals(object obj)
{
if (obj == null && this == null)
return true;
else if ( (obj == null && this != null ) || (obj != null && this == null))
return false;
else if (this.Brand == ((GridItem)obj).Brand && this.Category == ((GridItem)obj).Category)
return true;
else
return false;
}This first checks if the object are null, and then checks if the contents of the objects are the same. Now the above equation of g1.Equals(g2), will return true.