简体   繁体   English

C# 如何将列表中的一个值返回到 ComBox 中? 而不是所有

[英]C# how to return only one value from the list into a ComBox? Instead of all

I am aiming to return a single value of CustomerName into a ComboBox.我的目标是将CustomerName的单个值返回到 ComboBox。

This is how I get the data from the database:这就是我从数据库中获取数据的方式:

public class DataAccess
{
    public List<ComplaintModel> FindOrderNumber(string orderNumber)
    {
        using(SqlConnection conn = new SqlConnection(ConnectionString.MJMconnString))
        {
            var output = conn.Query<ComplaintModel>($@"
                    SELECT 
                    TRIM(p.description) as Product,
                    c.name AS CustomerName,
                    FROM SalesOrder so JOIN SalesOrderLine sol ON
                    so.id = sol.salesOrderID JOIN Customer c ON
                    so.customerID = c.id JOIN Product p ON
                    sol.productID = p.id WHERE so.number = '{orderNumber}'").ToList();
            return output;
        }
    }
}

This would be the output (Used Excel to show you a rough idea):这将是 output(使用 Excel 向您展示一个粗略的想法):

在此处输入图像描述

So the ComboBoxes would look like this:所以组合框看起来像这样:

在此处输入图像描述

在此处输入图像描述

Quick conclusion快速结论

The customer name will always be the same for specific OrderNumber but products vary.特定OrderNumber的客户名称将始终相同,但产品会有所不同。

Form形式

Within my form I load the Product and Customer combo-box.在我的表单中,我加载了ProductCustomer组合框。 ( Note: I know you may be thinking to use a TextBox instead of a ComboBox for Customer , it just needs to be). 注意:我知道您可能正在考虑为Customer使用 TextBox 而不是 ComboBox ,它只是需要)。

List<ComplaintModel> OrderNumber = new List<ComplaintModel>();

public void searchButton_Click(object sender, EventArgs e)
{
    clearBindings();

    DataAccess db = new DataAccess();
     
    // Search Order Number that is specified in a text box
    OrderNumber = db.FindOrderNumber(orderNumber.Text);
  
    // List all products
    product.DataSource = OrderNumber;
    product.DisplayMember = "Product";

    // Show customer name - but as [0] instead of all.
    customer_name.DataSource = OrderNumber;
    customer_name.DisplayMember = "CustomerName";
    

}

Desired output of combobox所需 output 的 combobox

在此处输入图像描述

This is my model这是我的 model

public class ComplaintModel
{
    public string CustomerName { get; set; }
    public string Product { get; set; }
}

Question

How do I retrieve just a single value of CustomerName into the ComboBox?如何将CustomerName的单个值检索到 ComboBox 中?

You need to create: Customer class, Order class.您需要创建:客户 class,订单 class。

Then You need to Create two lists for two comboboxes: List<Customer> customers for customer combobox List<Order> orders for orders combobox to show orders for selected customer (on combobox change event).然后您需要为两个组合框创建两个列表: List<Customer> customers for customer combobox List<Order> orders for orders combobox 以显示所选客户的订单(在 combobox 更改事件上)。

public class Customer
{
public string Id {get;set;}
public string Name {get;set;}
public List<Order> Orders {get;set;}
}

public class Order
{
public string Id {get;set;}
public string Name {get;set;}
public int CustomerId {get;set;}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM