【问题标题】:c# linq listView - selecteditem to objectc# linq listView - selecteditem to object
【发布时间】:2017-11-08 01:06:09
【问题描述】:

我需要将 listView.SelectedindexChanged 事件的一些变量发送到外部对象,就像我可以使用 listBox 一样。以下是我的 listView 代码。在此之后,我将上传外部对象代码和工作列表框代码。

private void listViewProducts_SelectedIndexChanged(object sender, EventArgs e)
{
    try
    {
        ProductList_Variables selected = (ProductList_Variables)listViewProducts.SelectedItems[0];

        textBoxProduct.Text = selected.Product;
        comboBoxCategory.SelectedItem = selected.Category;
        textBoxSize.Text = selected.Size.ToString();
        comboBoxMarket.SelectedItem = selected.Market;
        comboBoxContainer.SelectedItem = comboBoxContainer.Items.OfType<ProductList_Variables>().SingleOrDefault(x => x.Container == selected.Container);
        textBoxPrice1.Text = selected.Price.ToString();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

class ProductList_Variables
{
    public int Id { get; set; }
    public string Product { get; set; }
    public string Category { get; set; }
    public string Size { get; set; }
    public string Market { get; set; }
    public string ProductName { get { return Product + " - " + Category + " - Size: " + Size +", Market: "+ Market; } }
    public string Flavour { get; set; }
    public decimal Price { get; set; }
    public string Container { get; set; }
    public int IdContainer { get; set; }
}

    private void listBoxProducts_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            ProductList_Variables selected = (ProductList_Variables)listBoxProducts.SelectedItem;

            //textBoxProduct.Text = selected.Product;
            //comboBoxCategory.SelectedItem = selected.Category;
            //comboBoxMarket.SelectedItem = selected.Market;
            //comboBoxContainer.SelectedValue = selected.IdContainer;
            //textBoxPrice1.Text = selected.Price.ToString();

            textBoxProduct.Text = selected.Product;
            comboBoxCategory.SelectedItem = selected.Category;
            textBoxSize.Text = selected.Size.ToString();
            comboBoxMarket.SelectedItem = selected.Market;
            comboBoxContainer.SelectedItem = comboBoxContainer.Items.OfType<ProductList_Variables>().SingleOrDefault(x => x.Container == selected.Container);
            textBoxPrice1.Text = selected.Price.ToString();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

【问题讨论】:

  • 问题是什么?

标签: c# linq listview listbox


【解决方案1】:

如果您想为 SelectedIndexChanged 事件发送其他变量,您可能必须考虑编写符合这些要求的自己的事件。

你可以通过this user找到一个很好的解释。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多