Link to home
Start Free TrialLog in
Avatar of SJRAdmin
SJRAdmin

asked on

Set the value of a combo box

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

SOLUTION
Avatar of crysallus
crysallus
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SJRAdmin
SJRAdmin

ASKER

I will try it in the morning - thank you.
Avatar of Rahul Agarwal
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();
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
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!