Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 246
  • Last Modified:

How do I display unique items in Repeater?

I'm building a repeater, code listed below.  How do I only show unique items in the list?
0
JT_SIRO
Asked:
JT_SIRO
  • 3
1 Solution
 
MlandaTCommented:
No code posted ;)
0
 
JT_SIROAuthor Commented:
oops
        foreach (DataRow row in dt.Rows)
        {
            intCountBadCategories = intCountBadCategories + 1;

            // Check to see if this is a valid category
            if (IsValidCategory(row["Category"].ToString()) == false)
            {
                // Add to Repeater
                DataRow newRow3 = dt3.NewRow();
                newRow3["Category"] = row["Category"].ToString();
                dt3.Rows.Add(newRow3);
            }
        }

Open in new window

0
 
MlandaTCommented:
If dt is populated from the database, the best place would be to use a SELECT DISTINCT in the code loading data into the datatable, dt.
0
 
MlandaTCommented:
otherwise, check if the value already exists in dt3. Something like:
if (IsValidCategory(row["Category"].ToString()) == false && dt3.Select("Category = '" + row["Category"].ToString().Replace("'","''") + "'").Length = 0)

Open in new window

0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Hi, also can be done in this way (based on your example):
List<string> uniqueCategories = new List<string>();
foreach (DataRow row in dt.Rows)
{
    intCountBadCategories = intCountBadCategories + 1;

    string category = row["Category"].ToString();
    // Check to see if this is a valid category AND it dont exists in the list.
    if (!IsValidCategory(category) && !uniqueCategories.Contains(category))
        uniqueCategories.Add(category);
}
// Add to Repeater
foreach (string value in uniqueCategories)
    dt3.Rows.Add(value);

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now