Solved

Set the value of a combo box

Posted on 2011-02-23
6
398 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
Comment Utility
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
Comment Utility
I will try it in the morning - thank you.
0
 
LVL 13

Expert Comment

by:agarwalrahul
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 4

Accepted Solution

by:
fromer earned 400 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

11 Experts available now in Live!

Get 1:1 Help Now