【发布时间】:2023-03-07 14:45:01
【问题描述】:
我有一个带有列表作为数据源的组合框。此列表包含对象(客户)及其属性(名称、地址、...)。
当我选择组合框的一个项目时,我想将信息(地址、邮政编码...)传递给表单上的某些文本框。
在我的测试 1tier 应用程序中,这是正确的。
但是我正在开发的主要应用程序是基于 MVP 的(我自己对此有所了解)。我面临的问题是选角。由于我的观点不知道我的模型,我不应该被允许使用(客户)。 string address = ((Customers)comboBox1.SelectedItem).CustomerAddress;
1Tier 测试代码:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//getCustomers((int)comboBox1.SelectedValue);
//txtAddress.Text =Convert.ToString( comboBox1.SelectedValue);
Customers p = (Customers)comboBox1.SelectedItem;
string s = comboBox1.SelectedItem.ToString();
string address = ((Customers)comboBox1.SelectedItem).CustomerAddress;
txtAddress1.Text = address;
}
private void Form3_Load(object sender, EventArgs e)
{
using (var emp = new EmployerEFEntities())
{
var query = from customers in emp.Customers
select customers;
comboBox1.DisplayMember = "CustomerName";
comboBox1.ValueMember = "CustomerID";
comboBox1.DataSource = query.ToList();
}
}
我已经研究了几天了,但还没有成功。我希望有人能给我正确的方向。
真实应用的代码:
查看:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
txtName.Text = comboBox1.SelectedValue.ToString();
}
private void CustomerView_Load(object sender, EventArgs e)
{
comboBox1.DataSource = customerPresenter.getCustomers();
comboBox1.DisplayMember = "CustomerName";
comboBox1.ValueMember = "CustomerId";
}
演讲者:
public List<tbl_customer> getCustomers()
{
using (var customers = new DBCrownfishEntities())
{
var customer = from c in customers.tbl_customer
select c;
return customer.ToList();
}
}
【问题讨论】: