SSC
SSC

Reputation: 311

Initialize listbox in c# with a list

I want to initialize value and memberDisplay of my list box using the output of this code :

Here is my code :

    public List<showMaterialGroup> ShowSubGroup()
    {
        List<showMaterialGroup> q = (from i in dbconnect.tblMaterialGroups.AsEnumerable()
                                     where i.tenderId == _tenderId 

                                     select new showMaterialGroup()
                                         {
                                             id = i.id.ToString(),
                                             title = i.title,
                                             documentnumber = ReturnDocNumber(i.tenderId),

                                         }).ToList();
        return q;
    }

Here i call the function :

   txtgroup.DisplayMember = objtender.ShowSubGroup();
        txtgroup.ValueMember = objtender.ShowSubGroup();

So how can i do that?

Upvotes: 0

Views: 1095

Answers (2)

p.s.w.g
p.s.w.g

Reputation: 149040

I think you're confused about what these properties are supposed to do. DisplayMember and ValueMember are strings. They identify which properties of the items in the control's DataSource should represent the display content or value for each item.

Try this:

txtgroup.DataSource = objtender.ShowSubGroup();
txtgroup.DisplayMember = "title";
txtgroup.ValueMember = "id";

Upvotes: 0

Grant Winney
Grant Winney

Reputation: 66469

I'm assuming txtgroup is a ListBox, even though it's sorta named like a TextBox might be named. You can't assign a list to the DisplayMember and ValueMember properties.

Assign the list as the DataSource, then use the DisplayMember and ValueMember properties to specify which field should be displayed to the user and used as the actual value for the item in the list, respectively.

Try this instead:

txtgroup.DataSource = objtender.ShowSubGroup();

txtgroup.DisplayMember = "title";
txtgroup.ValueMember = "id";

Upvotes: 1

Related Questions