Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Adding items to combobox
Message
From
04/10/2005 00:44:00
 
 
To
03/10/2005 09:45:01
Keith Payne
Technical Marketing Solutions
Florida, United States
General information
Forum:
ASP.NET
Category:
Forms
Miscellaneous
Thread ID:
01054999
Message ID:
01055741
Views:
26
Keith,

Does that code work in VB? It needs tweaking for use with C# ... maybe I missed something or maybe it's different in VB, but I couldn't get it to work in C#. So, instead, I did this using CollectionBase:
public class MyPairCollection : CollectionBase
{
	public MyPair this[ int index ]  
	{
		get { return (MyPair)List[index]; }
		set { List[index] = value; }
	}

	public int Add( MyPair value )  
	{
		return( List.Add( value ) );
	}


}

public class MyPair
{
	private string m_MyDisplay = "";
	private int m_MyValue   = 0;

	public MyPair()
	{
		this.MyDisplay = "";
		this.MyValue   = 0;
	}

	public string MyDisplay
	{
		get {return this.m_MyDisplay;}
		set {this.m_MyDisplay = value;}
	}

	public int MyValue
	{
		get {return this.m_MyValue;}
		set {this.m_MyValue = value;}
	}
}
And then, to add the data and bind the Combo:
MyPairCollection o = new MyPairCollection();
o.Add(new MyPair());
o[0].MyDisplay = "One";
o[0].MyValue = 1;

o.Add(new MyPair());
o[1].MyDisplay = "Two";
o[1].MyValue = 2;

this.cboCode.DataSource = null;
this.cboCode.DataSource = o;
this.cboCode.DisplayMember = "MyDisplay";
this.cboCode.ValueMember   = "MyValue";
I think this ends up being a lot more work than creating a DataTable, unless I missed something and did it wrong (which is entirely possible).

~~Bonnie


>You could also use a NameObjectCollectionBase to hold the values, or a one-dimensional array of a custom objects to hold the display and value data:
>
>Public Class myPair
>Public Display as String
>Public Value as Integer
>End Class
>
>Dim mySource(10) as myPair
>
>' oops, forgot to instance the class
>mySource(0) = New myPair
>mySource(0).Display = 'One'
>mySource(0).Value = 1
>. . . . . .
>
>cboMyBox.DataSource = mySource
>cboMyBox.DisplayMember = "Display"
>cboMyBox.ValueMember = "Value"
>
>- Keith
>
>>Mike,
>>
>>The easiest way to accomplish this, IMHO, is to create a quickie DataTable to use as the Combo's DataSource. Quick code off the top of my head:
>>
>>DataTable dt = new DataTable();
>>dt.Columns.Add("MyDisplay", typeof(string));
>>dt.Columns.Add("MyValue", typeof(int));
>>
>>DataRow row = dt.NewRow();
>>row["MyDisplay"] = "One";
>>row["MyValue"] = 1;
>>dt.Rows.Add(row);
>>
>>row = dt.NewRow();
>>row["MyDisplay"] = "Two";
>>row["MyValue"] = 2;
>>dt.Rows.Add(row);
>>
>>// etc.etc.
>>
>>MyCombo.DataSource = dt;
>>MyCombo.DisplayMember = "MyDisplay";
>>MyCombo.ValueMember   = "MyValue";
>>
>>
>>~~Bonnie
>>
>>
>>
>>>Sorry....
>>>
>>>What I want to do is manually add items to a combobox, where the SelectedItem is One, Two, Three, Four and the SelectedValue is 1,2,3,4. I am having problems figuring this one out...
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform