如何在 C# 中为 Combobox 编写关系代码?

How do I write relation code for Combobox in C#?

请看一下这段代码:

void BindGrid()
{
    using (UnitOfWork db = new UnitOfWork())
    {
        cmbBook.DataSource = db.BookRepository.Get();
        cmbBook.DisplayMember = "Bookname";
        cmbBook.ValueMember = "BookId";
        lblid.Text = cmbStudy.SelectedValue.ToString();
    }
}

private void cmbBook_SelectedIndexChanged(object sender, EventArgs e)
{
    using(UnitOfWork db = new UnitOfWork())
    {
        lblID.Text =cmbBook.SelectedValue.ToString();
        cmbTeacher.DataSource = db.TecherRepository.Get().Where(p => p.BookTecherId == int.Parse(lblID.Text)).ToList;
        cmbTeacher.DisplayMember = "TeacherName";
        cmbTeacher.ValueMember = "TeacherId";
    }
}

我的 ComboBox 有一个关系,我想在用户从 cmbBook 中选择一个项目时填充 cmbTeacher 值,但是这个错误显示在 VS 中

System.ArgumentException: 'Complex DataBinding accepts as a data source either an IList or an IListSource.'

我是初学者英语不好,抱歉

您的错误是您的存储库很可能 returns 是 IEnumerable 类型,您需要 IListIListSource。 这可以通过调用方法 .ToList()

这是一个详细的示例,说明如何在项目选择上实现数据绑定和验证。

表单的外观如下:

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace BindDataExample
{
   
    public partial class Form1 : Form
    {
        public class Book
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public int TeacherId { get; set; }
        }

        public class Teacher
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }

        public class Repository
        {
            private readonly IEnumerable<Book> Books;
            private readonly IEnumerable<Teacher> Teachers;

            public Repository()
            {
                Teachers = new List<Teacher>()
            {
                new Teacher() {Id = 1, Name = "Bob"},
                new Teacher() {Id = 2, Name = "Alex"},
            };
                Books = new List<Book>()
            {
                new Book() {Id =1, Name = "Book A", TeacherId = 1},
                new Book() {Id =2, Name = "Book B", TeacherId = 2},
                new Book() {Id =3, Name = "Book C", TeacherId = 2},
                new Book() {Id =4, Name = "Book D", TeacherId = 1},
                new Book() {Id =5, Name = "Book E", TeacherId = 2},
            };
            }

            public IEnumerable<Book> GetBooks() => Books;
            public IEnumerable<Teacher> GetTeachers() => Teachers;
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            BindData(); // bind data
        }

        private void BindData()
        {
            var db = new Repository();
            // Bind teacher list in combobox
            comboBox.DataSource = db.GetTeachers().ToList();
            comboBox.DisplayMember = "Name";
            comboBox.ValueMember = "Id";
            comboBox.SelectedIndex = -1;
        }

        private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            // get selected value
            if(comboBox.SelectedItem is Teacher value)
            {
                var teacherId = value.Id;

                var db = new Repository();
                var books = db.GetBooks().Where(o => o.TeacherId == teacherId).ToList();

                textBox1.Text = books.Any() ? string.Join(", ", books.Select(o => o.Name)) : "the teacher has no books";
            }
            else
            {
                // select value is empty
                textBox1.Text = string.Empty;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            comboBox.SelectedIndex = -1;
        }
    }
}