rookie
rookie

Reputation: 45

How to not get repeated names appeared in the Combo Box c#

enter image description here

I encountered a problem where I can't seem to make the names in the Combo Box appear once instead of multiple one. Is there anything in my codes that causes this problem? Any help will be greatly appreciated.

Below is the code to link the names to the combo Box.

private void Create_EmpDetails_Load(object sender, EventArgs e)
    {
        using (satsEntities Setupctx = new satsEntities())
        {
            var viewEmpName = (from viewEN in Setupctx.employees
                               join ufi u in Setupctx.ufis on viewEN.UFISID equals u.UFISID
                               select new { u.EmployeeName , u.UFISID}).Distinct().ToList();

            cbName.DataSource = viewEmpName;
            cbName.DisplayMember = "EmployeeName";
            cbName.ValueMember = "EmployeeName";
            //cbName.ValueMember = "UFISID";

        }
    }

Upvotes: 0

Views: 1420

Answers (4)

Habib Zare
Habib Zare

Reputation: 1204

in combobaxes we display the string as DisplayMember for users and id of members(maybe important for us) as ValueMember for us. more time we work with ids . my example :

class Country
{
    public string Name { get; set; }
    public int ID { get; set; }
    public Country( int i,string s) { Name = s; ID = i; }
}

class ComboItem
{

    public string DisplayMember { get; set; }

    public int ValueMember { get; set; }

}


class ComboItemEqualityComparer : IEqualityComparer<ComboItem>
{

    public bool Equals(ComboItem item1, ComboItem item2)
    {
        if (item1.ValueMember == item2.ValueMember && item1.DisplayMember == item2.DisplayMember)
        {
            return true;
        }

        return false;
    }


    public int GetHashCode(ComboItem item)
    {
        string str = item.DisplayMember + item.ValueMember;
        return str.GetHashCode();
    }
}

test :

 List<Country> countries = new List<Country> {
                                  new Country(1,"UK"), 
                                  new Country(2,"Turkey"), 
                                  new Country(8,"Turkey"),
                                  new Country(5,"Turkey"), 
                                  new Country(2,"Turkey"),
                                  new Country(3,"USA") ,
                                  new Country(3,"USA")};  //.Distinct(new CountryEqualityComparer()).ToList();

        var Data = (from i in countries
                select new ComboItem { ValueMember = i.ID, DisplayMember = i.Name }).Distinct(new ComboItemEqualityComparer()).ToList();



        cbName.DataSource = Data;

        cbName.DisplayMember = "DisplayMember";
        cbName.ValueMember = "ValueMember";

sometimes we have data that have displayname the same but the id of them arent. we can change the ComboItemEqualityComparer equals method to :

public bool Equals(ComboItem item1, ComboItem item2)
    {
        if (item1.ValueMember == item2.ValueMember )
        {
            return true;
        }

        return false;
    }

enjoy.

for this question we can :

....
select new ComboItem { ValueMember = u.UFISID, DisplayMember = u.EmployeeName }).Distinct(new yourIEqualityComparer()).ToList();

Upvotes: 0

horgh
horgh

Reputation: 18553

Probably, it would be enough for you to replace

select new { u.EmployeeName , u.UFISID}

with

select new { u.EmployeeName }

Upvotes: 0

rookie
rookie

Reputation: 45

I edited to my codes to this and I managed to show only 1 name for instead of multiple records.

private void Create_EmpDetails_Load(object sender, EventArgs e)
    {
        using (satsEntities Setupctx = new satsEntities())
        {
            var viewEmpName = (from viewEN in Setupctx.employees
                               join ufi u in Setupctx.ufis on viewEN.UFISID equals u.UFISID
                               select new { u.EmployeeName }).Distinct().ToList();

            cbName.DataSource = viewEmpName;
            cbName.DisplayMember = "EmployeeName";
            cbName.ValueMember = "EmployeeName";
            //cbName.ValueMember = "UFISID";

        }
    }

Upvotes: 0

SLaks
SLaks

Reputation: 888185

Each of those rows has a different UFISID, so Distinct() is not removing them.

It sounds like you just want to show employees:

cbName.DataSource = Setupctx.Employees;

Upvotes: 2

Related Questions