473,320 Members | 1,939 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

ComboBox

I'm used to setting the Text property and Value property of a ComboBox. I
do not see this option for C# 2.0. Am I missing something?

Basically, I'm trying to loop thru an ADO.NET reader object and populating
the combobox with the "Text" and "Identity" column for a database.
Therefore, I need the ID so I can update the correct row.

Mar 22 '06 #1
4 4866
ComboBox never had this... not even in 1.1. The
ComboBox.ObjectCollection contains objects, not a specialized
ComboBoxItem that has Text and a Value. I always found this annoying,
since ListView had ListViewItem that had its text and a Tag field in
which you could store a key.

My impression was that you were supposed to solve this problem using
data bindings, but data bindings were a horrid mess in 1.1 (at least I
thought so).

Mar 22 '06 #2
Thanks -- I'm coming from a VB6 background. For some reason I assumed .NET
had it as the Data Binding to a Combobox seemed to have a selection for
setting this... guess it was an object or something.

In the meantime, I solved this by creating a simple class to hold my two
values. I didn't know if this was the normal way to handle this. Seemed
like a lot of work to me for something that should be relatively simple.

"Bruce Wood" <br*******@canada.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
ComboBox never had this... not even in 1.1. The
ComboBox.ObjectCollection contains objects, not a specialized
ComboBoxItem that has Text and a Value. I always found this annoying,
since ListView had ListViewItem that had its text and a Tag field in
which you could store a key.

My impression was that you were supposed to solve this problem using
data bindings, but data bindings were a horrid mess in 1.1 (at least I
thought so).

Mar 23 '06 #3
One thing that you can do is create an object that contains a property
returning text that should show in the ComboBox. Then you set the
ComboBox's DisplayMember to the name of that property, and populate the
ComboBox with these objects, rather than with strings.

I tried this a while back and it caused problems when clearing the
ComboBox of items and repopulating it (which I had to do in some
circumstances). I can't recall the precise details, but it didn't work
as cleanly as it could have.

Nonetheless, it's an option, particularly if you don't need to
repopulate the ComboBox.

Mar 23 '06 #4
On Wed, 22 Mar 2006 17:04:47 -0600, "dm1608" <dm****@spam.net> wrote:
I'm used to setting the Text property and Value property of a ComboBox. I
do not see this option for C# 2.0. Am I missing something?

Basically, I'm trying to loop thru an ADO.NET reader object and populating
the combobox with the "Text" and "Identity" column for a database.
Therefore, I need the ID so I can update the correct row.


Bruce is right there is no text and identity property in the .NET ComboBox.

One way around this is to create a class that you load with the reader. In fact
you can load a class that *IS* the object you are working with. All you need to
do is override the ToString() method of the class so it returns the text you
want the user to see. Then you can use the data contained by the class as you
want.

Here's an example:

// the display class can be used in any project needing to use combo boxes.

using System;
using System.Collections.Generic;
using System.Text;

namespace TestWindow
{
class DisplayObject
{
private string _displayText;
private int _id;

public DisplayObject()
{
}

public DisplayObject(int id, string displayText)
{
_displayText = displayText;
_id = id;
}

public string Text
{
get
{
return _displayText;
}
set
{
_displayText = value;
}
}

public int ID
{
get
{
return _id;
}
set
{
_id = value;
}
}

public override string ToString()
{
return _displayText;
}
}
}

// in the dialog containing the ComboBox...

private void TestDialog_Load(object sender, EventArgs e)
{
DisplayObject disp = new DisplayObject(1, "Test1");
comboBox1.Items.Add(disp);
disp = new DisplayObject(2, "Test2");
comboBox1.Items.Add(disp);
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
DisplayObject disp = (DisplayObject)comboBox1.SelectedItem;
int id = disp.ID;
string text = disp.Text;
}

HTH

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
Mar 23 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Mr. B | last post by:
Here's the situation... You've a combobox with Items already added. Say they look like this (or even lines of text): 10-00-232 10-00-256 10-01-006 10-01-213 10-02-200
7
by: Nicolae Fieraru | last post by:
Hi All, I am trying to change the rowsource of a combobox when I click on it. I played with many events, associated with the form and the combobox, but still haven't figured out what is the way...
8
by: Zlatko Matić | last post by:
There is a form (single form) and a combobox. I want that current record of the form is adjusted according to selected value in the combobox. Cuurrent record should be the same as the value in the...
1
by: anonymous | last post by:
I've been trying to put a them, please help me out. Here's the major parts of my code: public Form1() { DataSet myDataSet = new DataSet("myDataSet"); DataTable testTable = new...
3
by: TT (Tom Tempelaere) | last post by:
Hay there, I'm writing my own DataGridComboBoxColumn because .NET 1.1 does not have one (I hope .NET 2.0 supplies one). I based it on this article:...
2
by: Don | last post by:
I've looked high and low for some code that will allow me to have a combobox with a flat borderstyle. I found a few examples, but nothing that was really usable for me. I had the following...
4
by: jon f kaminsky | last post by:
Hi- I've seen this problem discussed a jillion times but I cannot seem to implement any advice that makes it work. I am porting a large project from VB6 to .NET. The issue is using the combo box...
6
by: dbuchanan | last post by:
VS2005 I've been reading all the help I can on the topic (MSDN, other) but I can't make sense of this. Desired behavior; The user is to choose from the displayed list of the databound combobox...
1
by: polocar | last post by:
Ciao a tutti, leggendo qua e lŕ per il forum ho scoperto che non sono l'unico ad avere questo problema. Se si inserisce un controllo ComboBox in un form di C#, non č possibile impostare la sua...
5
by: Rich | last post by:
Hello, I have a search application to search data in tables in a database (3 sql server tables). I populate 2 comboboxes with with data from each table. One combobox will contain unique...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.