1

I have a Struct that looks like this:

public struct Server
{
    public String ServerName, ServerUrl, ServerEnvironment;
};

Then I create a List of Structs:

List<Server> MyServers = new List<Server>();

...and read about eight records into it from a small XML file. This is working well, and if I hover over "MyServers" in the debugger, it looks something like this:

MyServers Count=8
  [0] {MyApp.Server}
    ServerEnvironment   "DEV"
    ServerName          "My Dev Server #1"
    ServerUrl           "https://mydev1.mycompany.com/"
  [1] {MyApp.Server}
etc...

Then if I do something like MessageBox.Show(MyServers[0].ServerName);, it displays the expected value.

Now what I would like to do is create a ComboBox from the ServerName fields. When the user selects one, I think I would then use the SelectedIndex property of the ComboBox to access the other information (ServerUrl and ServerEnvironment) for the selected ServerName.

I thought I could do something like this:

comboBoxServers.DataSource = MyServers ... something ... ServerName;

But I can't seem to find anything that works. Is this even possible, or do I need to create a separate, simple List with only the ServerName strings and use that for the ComboBox DataSource?

3 Answers 3

4

You can use only DisplayMember to show names as Text in combobox.
Then comboBox.SelectedValue will return whole object.

But for using DisplayMember you need change field ServerName to the property

public struct Server
{
    public string ServerName { get; set; }
}

comboBoxServers.DisplayMember = "ServerName";
comboBoxServers.DataSource = MyServers;

If you don't want change fields of struct to the property you can override ToString method for your struct.
ComboBox simply calling .ToString() on every item in the datasource, if DisplayMember not assigned, for generating Text of item.

public override string ToString()
{
    return Name;
}

Then you can access selected server information through comboBoxServers.SelectedValue which return whole instance of Server

var selectedServer = (Server)comboBoxServers.SelectedValue;
selectedServer.ServerUrl;
selectedServer.ServerEnvironment; // ...
Sign up to request clarification or add additional context in comments.

3 Comments

This sounds like a good idea, but when I tried adding { get; set; } to the ServerName declaration (public String ServerName { get; set; }), I got a compiler error where I tried to assign a value to it: Server TempServer; TempServer.ServerName = (value); TempServer.ServerUrl = (value); TempServer.ServerEnvironment = (value); MyServers.Add(TempServer);. The error was Use of unassigned local variable 'TempServer'. This was working before I added the { get; set; }. Do I need to change the way I am assigning a value?
For using properties you need use Server TempServer = new Server();, but you can just override ToString() as another aproach.
Hurray! { get; set; } with Server TempServer = new Server(); worked. Thank you for helping me navigate all the tricky details. Are there advantages to using ToString() instead, or any circumstances where that would be preferred? Or are both methods equally good?
1

Create List Server:

List<Server> Servers= new List<Server>()
{
    new Server  {ServerEnvironment = "DEV", ServerName = "My Dev Server #1"},
    new Server  {ServerEnvironment = "DEV1", ServerName = "My Dev Server #2"},
};

Then add list to comboBox:

comboBoxServers.DataSource = Servers;
comboBoxServers.ValueMember = "ServerEnvironment "; 
comboBoxServers.DisplayMember = "ServerName" ;

4 Comments

This will not work because OP's struct have only fields. DataBinding working only with properties.
Thanks for the suggestion. I tried this, but got this error: Cannot bind to the new display member.
Do you add properties for list : public string ServerEnvironment { get; set; } public string ServerName { get; set; }
I tried adding { get; set; } to the properties, but this led to a different error. In the place where I assign a value to a temporary Server struct (called TempServer) before adding it to the MyServers list, I got the compiler error: Use of unassigned local variable 'TempServer'. This worked before. Do I need to change the way I assign values when I use { get; set; }?
0

This might do the trick for you

comboBoxServers.ValueMember = ServerUrl; 
comboBoxServers.DisplayMember = ServerName;
comboBoxServers.DataSource = MyServers;

DisplayMember: Gets or sets the property to display for any ListControl. The String specifying the name of an object property that is contained in the collection specified by the DataSource property.

ValueMember: Gets or sets the path of the property to use as the actual value for the items in the ListControl. This String representing a single property name of the DataSource property value, or a hierarchy of period-delimited property names that resolves to a property name of the final data-bound object.

So now when the user selects one in the comboBoxServers you can access the other information (ServerUrl and ServerEnvironment) for the selected ServerName like

string urlenv = comboBoxServers.SelectedValue;
string serName = comboBoxServers.Text;

1 Comment

From DisplayProperty ...specifying the name of an object property... so it will not work with fields.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.