Ich habe eine Combobox mit einer Liste als Datenquelle. Diese Liste enthält Objekte (Kunden) mit ihren Eigenschaften (Name, Adresse, ...). Wenn ich ein Element der Combobox auswähle, möchte ich die Informationen (Adresse, Postleitzahl ...) an einige Textfelder in meinem Formular weitergeben. In meinem Test 1tier Anwendung funktioniert das richtig. Aber die Hauptanwendung, die ich arbeite, basiert auf MVP (mit meiner eigenen Berührung darauf). Das Problem, dem man sich gegenübersieht, ist das Gießen. Da meine Ansicht mein Modell nicht kennt, sollte ich die (Kunden) nicht verwenden dürfen. string address = ((Customers)comboBox1.SelectedItem).CustomerAddress;
MVP Winforms und Textfeld Combobox Werte
1fach Testcode:
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();
}
}
Ich habe jetzt um diese für ein paar Tage suchen, sondern haben zu einem Erfolg kommen. Ich hoffe, jemand könnte mir die richtige Richtung geben.
Der Code der realen Anwendung:
Ausblick:
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";
}
Moderator:
public List<tbl_customer> getCustomers()
{
using (var customers = new DBCrownfishEntities())
{
var customer = from c in customers.tbl_customer
select c;
return customer.ToList();
}
}