0

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";

        }
    }
LOZ
  • 1,169
  • 2
  • 16
  • 43
rookie
  • 45
  • 1
  • 9

4 Answers4

2

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;
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
0

Probably, it would be enough for you to replace

select new { u.EmployeeName , u.UFISID}

with

select new { u.EmployeeName }
horgh
  • 17,918
  • 22
  • 68
  • 123
  • This is the error it prompt when I tried your way. "Cannot bind to the new display member. Parameter name: newDisplayMember" – rookie Aug 13 '12 at 01:59
  • The data in the combo box should be only appearing 1 particular name instead of multiple times. – rookie Aug 13 '12 at 02:01
  • and if `select new { u.EmployeeName }`? – horgh Aug 13 '12 at 02:01
  • @habibzare, "but withoutno UFISID" - what do you mean by that? – horgh Aug 13 '12 at 02:30
  • we want to display employe name in combobox but every members have id.we work with ids. the ids are important for us. – Habib Zare Aug 13 '12 at 03:06
  • @habibzare The author never said that ids were of any importance in this very issue; besides it is quite another question how to let the user determine which row exactly he wants to choose...I'd say that only Id + Name is obviously not enough for that.. – horgh Aug 13 '12 at 03:54
0

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";

        }
    }
rookie
  • 45
  • 1
  • 9
0

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();
Habib Zare
  • 1,206
  • 8
  • 17
  • The problem is there can be multiple UFISIDs per employee. What you want is the employee's Id. – Risky Martin Aug 13 '12 at 19:56
  • So you are doing it correctly for your project, but in the example you gave you used UFISID, which is not the employee's Id. – Risky Martin Aug 14 '12 at 12:45
  • For avoid create **ComboItemEqualityCompare** class, maybe useful create ***InlineComparer*** : `http://stackoverflow.com/questions/5969505/using-iequalitycomparer-for-union // http://stackoverflow.com/questions/188120/can-i-specify-my-explicit-type-comparator-inline // http://stackoverflow.com/questions/1300088/distinct-with-lambda/ ` – Kiquenet Oct 15 '14 at 10:58