Solved

Set the value of a combo box

Posted on 2011-02-23
6
399 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

863 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

20 Experts available now in Live!

Get 1:1 Help Now