Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Replace ComboBox ListIndex

Posted on 2004-08-25
3
Medium Priority
?
1,258 Views
Last Modified: 2008-02-01
Hey Experts,

The following code is from VB6, and it actually replace the ListIndex of a ComboBox with my table's primary Key.
Does someone knows of any way to do this in C# ?


mission.AddItem tb_mission("mission_type_name_e")
mission.ItemData(mission.NewIndex) = Val(tb_mission("mission_type_id"))

val_mission_type = combo_mission_type.ItemData(combo_mission_type.ListIndex)
0
Comment
Question by:Francis_B
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 1

Expert Comment

by:chmohan
ID: 11897011
use combobox.SelectedIndex in c#  .net
0
 
LVL 19

Accepted Solution

by:
drichards earned 2000 total points
ID: 11897133
You don't do things quite the same way with a .NET ComboBox.  Instead if having strings in the list and itemData for each item, .NET puts an object in the list and uses the default string representation (ToString() method) of the object for display.  So, this means in .NET (C# in your case) you need to create a class that holds the string value and key value from the data table.  The ToString() method should return the string value.  For each item in the combobox, you create a new instance of the object and do 'comboBox1.Add(newItem);' where newItem is an object instance.   It will look something like this:

        class CBItem
        {
            public CBItem(string name, int key) { m_name = name; m_key = key; }
            public override string ToString() { return m_name; }
            public int Key() { return m_key; }

            private int m_key;
            private string m_name;

        }


Then in your code loading the combobox:

   comboBox1.Items.Add(new CBItem(<db string>, <db key>)); // replace <db name> and <db key> with appropriate values

Then to view "item data":

   int key = ((CBItem)comboBox1.Items[idx]).Key();
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question