Fill a ComboBox

  • Thread starter Thread starter Pepehammer
  • Start date Start date
P

Pepehammer

Hi everybody!

I got a combobox to fill.
I want to display some information (Text), but when I select a item, I want
to return a number.

The info I want to fill in is something like this:

TEXT VALUE
Category1 10
Category2 15
Category3 35

How can I perform this task?

Thanks!
 
Hi,

Yes, one way would be to create a DataTable containing two columns for the
datasource and set DisplayMember and ValueMember accordingly:
DataTable table = new DataTable();

table.Columns.Add("Id", typeof(int));

table.Columns.Add("Description", typeof(string));

table.Rows.Add(new object[]{1, "One"});

table.Rows.Add(new object[]{2, "Two"});

comboBox1.ValueMember = "Id";

comboBox1.DisplayMember = "Description";

comboBox1.DataSource = table;
 
A simple way is to create a class to hold your data. The combobox uses the
System.Windows.Forms.ComboBox.ObjectCollection class to hold the items for
the combobox and uses the ToString() method to get the text to display.
Simply create a class that has a String and an Integer and add a ToString()
method to return the String value.

See this previous newsgroup post...

http://groups.google.com/[email protected]&rnum=1
 
How i fill the combobox with info from a database (SQL)?

Thanks


Miha Markic said:
Hi,

Yes, one way would be to create a DataTable containing two columns for the
datasource and set DisplayMember and ValueMember accordingly:
DataTable table = new DataTable();

table.Columns.Add("Id", typeof(int));

table.Columns.Add("Description", typeof(string));

table.Rows.Add(new object[]{1, "One"});

table.Rows.Add(new object[]{2, "Two"});

comboBox1.ValueMember = "Id";

comboBox1.DisplayMember = "Description";

comboBox1.DataSource = table;


--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com



Pepehammer said:
Hi everybody!

I got a combobox to fill.
I want to display some information (Text), but when I select a item, I want
to return a number.

The info I want to fill in is something like this:

TEXT VALUE
Category1 10
Category2 15
Category3 35

How can I perform this task?

Thanks!
 
This should give you what you need:

http://www.csharphelp.com/archives/archive151.html

MOOGY!

Pepehammer said:
How i fill the combobox with info from a database (SQL)?

Thanks


Miha Markic said:
Hi,

Yes, one way would be to create a DataTable containing two columns for the
datasource and set DisplayMember and ValueMember accordingly:
DataTable table = new DataTable();

table.Columns.Add("Id", typeof(int));

table.Columns.Add("Description", typeof(string));

table.Rows.Add(new object[]{1, "One"});

table.Rows.Add(new object[]{2, "Two"});

comboBox1.ValueMember = "Id";

comboBox1.DisplayMember = "Description";

comboBox1.DataSource = table;


--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com



Pepehammer said:
Hi everybody!

I got a combobox to fill.
I want to display some information (Text), but when I select a item, I want
to return a number.

The info I want to fill in is something like this:

TEXT VALUE
Category1 10
Category2 15
Category3 35

How can I perform this task?

Thanks!
 
Hi,

You don't need database - you just use a class DataTable which is not linked
to database.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

Pepehammer said:
How i fill the combobox with info from a database (SQL)?

Thanks


Miha Markic said:
Hi,

Yes, one way would be to create a DataTable containing two columns for the
datasource and set DisplayMember and ValueMember accordingly:
DataTable table = new DataTable();

table.Columns.Add("Id", typeof(int));

table.Columns.Add("Description", typeof(string));

table.Rows.Add(new object[]{1, "One"});

table.Rows.Add(new object[]{2, "Two"});

comboBox1.ValueMember = "Id";

comboBox1.DisplayMember = "Description";

comboBox1.DataSource = table;


--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com



Pepehammer said:
Hi everybody!

I got a combobox to fill.
I want to display some information (Text), but when I select a item, I want
to return a number.

The info I want to fill in is something like this:

TEXT VALUE
Category1 10
Category2 15
Category3 35

How can I perform this task?

Thanks!
 
Hey Miha, did you see my post about how to receive event changes from the
SQL server?

Nobody has really responded. Would be nice to get some help on that...

MOOGY!
 
This seems a rather complex solution for enterprise-scale projects; my
client is a simple connection client for a small custom database, yet we may
have 3-4 people working on it at the same time.

Don't get me wrong, if this is the only way, I'll definitely have to pursue
it; I was just hoping that I could use triggers and an existing SQL
connection to communicate, or something along those lines.

I'm wondering if writing a RPC service may be the easier solution. I was
just hoping to use something internal to SQL...

There's no way to receive simple trigger messages through events that SQL
Server can raise through the connection?

MOOGY!



Miha Markic said:
Hi Moogy,

William gave you a good answer.
If you want learn more on notification services, read online docs:
http://msdn.microsoft.com/library/?...tsv/htm/ns_gettingstarted_4mn9.asp?frame=true

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

Moogy said:
Hey Miha, did you see my post about how to receive event changes from the
SQL server?

Nobody has really responded. Would be nice to get some help on that...

MOOGY!
 
Hi Moogy,

Moogy said:
This seems a rather complex solution for enterprise-scale projects; my
client is a simple connection client for a small custom database, yet we may
have 3-4 people working on it at the same time.

Yes, it is failry complex I guess - ado.net 2 will ease it a lot.
Don't get me wrong, if this is the only way, I'll definitely have to pursue
it; I was just hoping that I could use triggers and an existing SQL
connection to communicate, or something along those lines.

No, not through connection. There is no way afaik.

I'm wondering if writing a RPC service may be the easier solution. I was
just hoping to use something internal to SQL...

No, there is no such thing.
There's no way to receive simple trigger messages through events that SQL
Server can raise through the connection?

You won't be able to use connection. However, you might create an extended
stored procedure (invoked from anywhere, by trigger for example) that does
whatever you want.
The drawback is that it has to be a simple dll thus it has to be writen in
VisualC++ (you can still use managed extensions though) - not hard as it
seems. :).
 
Back
Top