简体   繁体   English

组合框多值成员单个显示成员c#

[英]combobox multiple valuemember single displaymember c#

I have data like this... 我有这样的数据...

id->name->floor
1->store1->1
2->store2->1
3->store2->2
4->store2->3
5->store3->2

I want add it in the combobox with display like this... 我想将其添加到带有这样的显示的combobox ...

store1          
store2          
store3          

When I choose store2 I want get valuemember = 2, 3, 4 当我选择store2时我想获得valuemember = 2, 3, 4

I think a logic (I'm not yet try at code)... 我认为是一种逻辑(我尚未尝试编写代码)...
When I choose store2 I just get valuemember = 2 after that I check at database which id have same name in table. 当我选择store2时,我只是得到valuemember = 2之后我检查databaseid在表中是否具有相同name But I have 500++ data, so I think it's to waste process 但是我有500 ++数据,所以我认为这是浪费过程

Any idea?? 任何想法??

note : 注意 : 例 this is just sample form, i don't know how to code, i just make design 这只是示例表格,我不知道如何编码,我只是进行设计
it will be helpfull if you give me implementation to combobox not to cmd 如果您给我实现组合框而不是cmd会有所帮助

Do you want to be easy to retrieve the data in selected items? 您是否想轻松地检索选定项目中的数据? I prefer to fill data with its special type so that can get data directly from seleted items. 我更喜欢用特殊类型填充数据,以便可以直接从选中的项目中获取数据。 because the Combox.Item.Add() function needs a prameter in Object type, so, I suggest that you can firstly define your data in a new type, like this: 因为Combox.Item.Add()函数需要Object类型的参数,所以,我建议您首先可以以新类型定义数据,如下所示:

/// <summary>
/// my own data define
/// </summary>
public class MyFloor
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Floor { get; set; }

    //very important, the result will be displayed in the combox
    public override string ToString()
    {
        return string.Format("{0}->{1}->{2}", ID, Name, Floor);
    }
}

then, you can fill the data into combox in special type: 然后,您可以将数据以特殊类型填充到combox中:

void FillData()
    {
        //load data from txt or database
        List<MyFloor> floorList = new List<MyFloor>(){
            new MyFloor{ID=1, Name="store1", Floor="1"},
            new MyFloor{ID=2, Name="store2", Floor="1"},
            new MyFloor{ID=3, Name="store2", Floor="2"},
            new MyFloor{ID=4, Name="store2", Floor="3"},
            new MyFloor{ID=5, Name="store3", Floor="2"}
            };
        //fill into combox
        foreach (MyFloor floor in floorList)
        {
            this.comboBox1.Items.Add(floor);
        }
    }

at last, you can get your data directly: 最后,您可以直接获取数据:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        //retrieve data from selected item as MyFloor object
        MyFloor floor = this.comboBox1.SelectedItem as MyFloor;
        //show the selected data object
        if (floor != null)
        {
            txtID.Text = floor.ID.ToString();
            txtName.Text = floor.Name;
            txtFloor.Text = floor.Floor;
        }
    }

here is my result: 这是我的结果: 结果展示

You can group the data before putting them into the ComboBox. 您可以在将数据放入组合框之前对其进行分组。 I've put together a small sample (follow the link to run or change it): 我整理了一个小示例 (按照链接进行运行或更改):

using System;
using System.Collections.Generic;
using System.Linq;

    public class MyData
    {

    public int Id { get; set; }
    public string Name { get; set; }
    public int Floor { get; set; }
}

public class MyGroupedData
{
    public string Name { get; set; }
    public IEnumerable<int> Floors { get; set; }
}

public class Test
{
    public static void Main()
    {
        MyData[] data = { 
            new MyData() { Id = 1, Name = "Store1", Floor = 1 }, 
            new MyData() { Id = 2, Name = "Store2", Floor = 1 }, 
            new MyData() { Id = 3, Name = "Store2", Floor = 2 }, 
            new MyData() { Id = 4, Name = "Store2", Floor = 3 }, 
            new MyData() { Id = 5, Name = "Store3", Floor = 2 },
        };
        var groupedData = from x in data group x by x.Name into grp 
            select new MyGroupedData() { Name = grp.Key, Floors = grp.Select(y => y.Floor) };
        foreach(var g in groupedData)
            Console.WriteLine("{0} -> {1}", g.Name, string.Join(", ", g.Floors.Select(x => x.ToString()).ToArray()));
    }
}

Though this sample is a Console application, I hope it shows the main steps you need to solve your problem. 尽管此示例是一个Console应用程序,但我希望它显示解决问题所需的主要步骤。 You bind the ComboBox to the result of the grouping operation and access the selected item by using the SelectedItem property. 您将ComboBox绑定到分组操作的结果,并使用SelectedItem属性访问所选项目。 Also, you can set the value member to "Floors" in order to use the SelectedValue property. 另外,您可以将值成员设置为“ Floors”,以使用SelectedValue属性。
In addition, you set the display member to "Name" so that the ComboBox shows the name. 另外,将显示成员设置为“名称”,以便组合框显示名称。
One thing to note is that when grouping, you loose the id of the records. 要注意的一件事是,在分组时,您会丢失记录的ID。 If this is a valuable information in this case, you'd need to adjust the grouping statement so that it not only takes the floor, but also the id into the group items. 如果在这种情况下这是有价值的信息,则您需要调整分组语句,以使其不仅占据发言权,而且将ID放入分组项目中。 If the id is the same for a store name (which is not the case in your sample data), you could add an Id property to the MyGroupedData class. 如果商店名称的ID相同(示例数据中的ID则不是),则可以向MyGroupedData类添加Id属性。

.NET has the capability to accept LINQ Results, Lists, DataSets, etc. in the DataSource property. .NET可以在DataSource属性中接受LINQ结果,列表,数据集等。 You could do something like this: 您可以执行以下操作:

    comboBox.DataSource = DataSetObject;
    comboBox.DisplayMember = "DataTableFromDataSetObject.nameColumn";
    comboBox.ValueMember = "DataTableFromDataSetObject.idColumn";
    /*-------OR LINQ OBJECT-----------*/

    comboBox.DataSource = (from dt.Select() in row  
                        select new {name = row["nameColumn"],id = row["idColumn"]}).ToList();
    comboBox.DisplayMember = "nameColumn";
    comboBox.ValueMember = "idColumn";

I hope this should be useful to your ask 我希望这对您的询问有用

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

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