开发者

Using Linq to Entities to poulate comboBoxes in a winforms application, one comboBox dependent on the other

开发者 https://www.devze.com 2023-04-06 03:15 出处:网络
Here is my code; comboBoxAdminVisit.DataSource = be.Events; comboBoxAdminVisit.DisplayMember = \"EventName\";

Here is my code;

        comboBoxAdminVisit.DataSource = be.Events;
        comboBoxAdminVisit.DisplayMember = "EventName";


        var fff = (from cc in be.Visitors
                  select cc.Attending).FirstOrDefault();

        var ggg = (from xx in be.Events
                   where xx.Id == fff
                   select xx.Id).FirstOrDefault();

        if (fff == ggg)开发者_如何学运维
        {
            foreach (var name in comboBoxAdminName.Items)
            {
                comboBoxAdminName.Items.Add(name);
            }

cc.Attending is a foreign key, it stores the priomary key of the event tabel. I want to select an event from the first combo box and have the second populated with those attending the event. Thanks in advance!

Events       Visitors
------       --------
Id           Id
Attendee(FK) Name
EventName    Company
EventStart   Car Reg
EventEnd     Visiting


I don't know exact structure of your entities, but here is an idea how to implement it:

private void Form1_Load(object sender, EventArgs e)
{
    comboBoxAdminVisit.DataSource = be.Events;
    comboBoxAdminVisit.DisplayMember = "EventName";
}

private void comboBoxAdminVisit_SelectedIndexChanged(object sender, EventArgs e)
{
    if (comboBoxAdminVisit.SelectedItem != null)
    {
        Event selectedEvent = (Event)comboBoxAdminVisit.SelectedItem;

        var visitors = (from cc in be.Visitors
                        where cc.Attending.Events.Contains(x => x.EnventId = selectedEvent.Id)
                        select cc);

        comboBoxAdminName.DataSource = visitors;
        comboBoxAdminName.DisplayMember = "Name";
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号