1

我是 C# 和 GUI 编程的新手,所以我觉得这很简单,但是 DisplayMember 上的文档让我感到困惑。

List<string[]> serverInfo = new List<string[]>();
serverInfo.Add(new string[] { "server one", "1.2.3.4", "12345" });
serverInfo.Add(new string[] { "server two", "1.2.3.4", "12346" });
serverInfo.Add(new string[] { "server three", "1.2.3.5", "12343" });

ComboBox serverPickList = new ComboBox();
serverPickList.DataSource = serverInfo;
serverPickList.DisplayMember = "???";

ComboBox 的选项应该是“服务器一”、“服务器二”和“服务器三”,但我仍然希望绑定字符串 [] 数组,因为我将处理它们。

4

2 回答 2

3
serverPickList.DataSource = serverInfo.Select(x => new { ColA = x[0], ColB =x[1], ColC=x[2] }).ToList();
serverPickList.DisplayMember = "ColA";
于 2012-05-27T05:10:26.703 回答
1

你为什么不开个课呢?

class ServerInfo
{
    public string Name { get; set; }
    public string OtherStuff { get; set; }
    public string OtherStuff2 { get; set; }

    public ServerInfo(string name, string otherStuff, string otherStuff2)
    {
        this.Name = name;
        this.OtherStuff = otherStuff;
        this.OtherStuff2 = otherStuff2;
    }
}

进而:

List<ServerInfo> serverInfo = new List<ServerInfo>();
serverInfo.Add(new ServerInfo("server one", "1.2.3.4", "12345"));
serverInfo.Add(new ServerInfo("server two", "1.2.3.4", "12346"));
serverInfo.Add(new ServerInfo("server three", "1.2.3.5", "12343"));

ComboBox serverPickList = new ComboBox();
serverPickList.DataSource = serverInfo;
serverPickList.DisplayMemeber = "Name";
于 2012-05-27T05:07:00.657 回答