Solved

Set the value of a combo box

Posted on 2011-02-23
6
400 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
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
DevOps Toolchain Recommendations

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

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

816 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now