Solved

Set the value of a combo box

Posted on 2011-02-23
6
403 Views
Last Modified: 2013-12-17
Hi I need to set the value of a combo box.

We are using Visual Studio 2010 WINDOWS Forms Application

Below is an example of the public class called ListLoader that we use to populate all of our combo boxes. It works great, and below the class you will see some code that we use to populate the combo box based on a recordset...still works great. Our problem is based on the class that we are using we have no idea how to:

cboGroups.SelectedItem = 5;

We do not know how to set the selected item, or the selected value or whatever is required so that we can set the initial selected item to a predefined selected item. Any suggestions??
//this is code from a public class we use for all combo boxes
//-------------------------------------------------------  

  public class ListLoader
    {
        //the ListLoader class is to be used by all comboboxes and listboxes so that they may be populated with text and show values or id's

        string iID = "";
        string iName = "";

        public string ItemID
        {
            get { return iID; }
        }
        public string ItemName
        {
            get { return iName; }
        }

        public ListLoader(string ItemID, string ItemName)
        {
            iID = ItemID;
            iName = ItemName;
        }

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



//load the combo box with values from the database
//-------------------------------------------------------------
            cboGroups.Items.Clear();

            SqlConnection cn = new SqlConnection(PubVar.dbConn);
            cn.Open();

            SqlCommand cmd = new SqlCommand(PubVar.dbConn, cn);
            cmd.CommandText = "spLookupSecurityGroups";
            cmd.CommandType = CommandType.StoredProcedure;

            SqlDataReader rdr = cmd.ExecuteReader();

            cboGroups.DisplayMember = "ItemName";
            cboGroups.ValueMember = "ItemID";

            //populate the combobox using the ListLoader class
            while (rdr.Read())
            {
                cboGroups.Items.Add(new ListLoader(rdr["GroupID"].ToString(), rdr["GroupName"].ToString()));
            }

            cn.Close();
            cmd.Dispose();

Open in new window

0
Comment
Question by:SJRAdmin
[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
6 Comments
 
LVL 8

Assisted Solution

by:crysallus
crysallus earned 100 total points
ID: 34966548
You need to override the Equals method in your ListLoader class. Then assign SelectedItem a new ListLoader object with the same values as the one you wish to select. Only by defining the Equals method can the combobox know how to match your selected item to the ones in the list.

eg. in ListLoader

public override bool Equals(object obj)
{
	return iID.Equals(((ListLoader)obj).ItemID) && iName.Equals(((ListLoader)obj).ItemName);
}

Open in new window


Then set SelectedItem to a ListLoader object with whatever values you want to select:

cboGroups.SelectedItem = new ListLoader("5", "Name 5");

Open in new window

0
 

Author Comment

by:SJRAdmin
ID: 34966757
I will try it in the morning - thank you.
0
 
LVL 13

Expert Comment

by:agarwalrahul
ID: 34967330
Try This may be this help you out

 cboGroups.Items.Clear();

            SqlConnection cn = new SqlConnection(PubVar.dbConn);
            cn.Open();

            SqlCommand cmd = new SqlCommand(PubVar.dbConn, cn);
            cmd.CommandText = "spLookupSecurityGroups";
            cmd.CommandType = CommandType.StoredProcedure;

            SqlDataReader rdr = cmd.ExecuteReader();

            cboGroups.DisplayMember = "ItemName";
            cboGroups.ValueMember = "ItemID";

            //populate the combobox using the ListLoader class
            while (rdr.Read())
            {
                cboGroups.Items.Add(new ListLoader(rdr["GroupID"].ToString(), rdr["GroupName"].ToString()));
            }
            cboGroups.SelectedValue = 5
            cn.Close();
            cmd.Dispose();
0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 4

Accepted Solution

by:
fromer earned 400 total points
ID: 34969100
addition to crysallus:

i think this will work much better;

        public override bool Equals(object obj)
        {
            if (obj != null)
            {
                return iID == obj.ToString();
            }
            else
            {
                return iID == null; // or just return false;
            }
        }

0
 

Author Comment

by:SJRAdmin
ID: 34971455
Hi, right now based on what you guys are saying it seems as though I have to supply both the ID and the Value to match against. In my stored procedure that gives me the current record, I only have the ID to compare against. I have no way of comparing both the ID and the Value. Is it possible to have it only test against the ID?
0
 

Author Closing Comment

by:SJRAdmin
ID: 34971541
Thank you both for your input. It worked.

When we went back to test against using only the id it worked with this line of code:

cboCategory.SelectedItem = new ListLoader(rdr["CategoryID"].ToString(), null);


I was able to specify null and have it still return a match based on ID.

Thanks!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

740 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