Noor
Noor

Reputation: 305

How to set the exact dropdownlist item that matches a given string?

I am working on a Windows Form Application where I have to edit a given record. This record also contains a dropdown list. What I want to do is to allow the user to edit the dropdown list item as well along with the rest of the record.

I have queried the required dropdown list item from the database that the user has to edit and assign that to a string variable like as follow:

DataTable dtMName = Products.SelectByManufacturerId(manufacturerId);
               if (dtMName.Rows.Count > 0)
               {
                   foreach (DataRow item in dtMName.Rows)
                   {
                       string manufacturerName = item[0].ToString();
                   }  
               }

Now, within the foreach loop (as the returned data row just have a single manufacturer name which the user will edit later), I want to select the dropdown list item that matches the string manufacturerName.

As far, I have tried the following code but no luck.

childEditProduct.cmbManufacturer.SelectedIndex = childEditProduct.cmbManufacturer.FindString(manufacturerName);

Is there any problem with this approach for selecting the matching dropdown list item? or is there any effective way that can help in my case? Any help will highly be appreciated!

Upvotes: 1

Views: 920

Answers (5)

Noor
Noor

Reputation: 305

As I was loading the manufacturer dropdown list on my childForm load event using the following function:

private void LoadManufacturers()
    {
        cmbManufacturer.Items.Clear();
        DataTable dtManufacturers = DataAccess.Select("select manufacturername from tblmanufacturer order by manufacturername asc");
        foreach (DataRow dr in dtManufacturers.Rows)
        {
            cmbManufacturer.Items.Add(dr[0].ToString());
        }
        //cmbManufacturer.SelectedIndex = 0;
    }

While loading manufacturers on the page load event, I was also specifying the selected index of the dropdown list using the code cmbManufacturer.SelectedIndex = 0; which I commented out later.

Once done, I then recalculate (re-query) the selected manufacturer (which the user want to update) within the load event of the dropdown list like this:

private void frmChildEditProduct_Load(object sender, EventArgs e)
    {
        LoadManufacturers(); // Calling the loadManufacturers function to load all the manufacturers to the dropdown list.

        string manufacturerID = lblManufacturerId.Text;

        DataTable dtMName = Products.SelectByManufacturerId(manufacturerID);
                if (dtMName.Rows.Count > 0)
                {
                    foreach (DataRow manufacturer in dtMName.Rows)
                    {
                        string manufacturerName = manufacturer[0].ToString();
                        cmbManufacturer.SelectedIndex = Convert.ToInt32(cmbManufacturer.FindStringExact(manufacturerName));
                    }
                }
     }

And my problem was solved. Thanks for the support I got on Stackoverflow.

Upvotes: 0

Thomas Gales
Thomas Gales

Reputation: 137

If your items are strings, try using Combobox.SelectedItem instead:

childEditProduct.cmbManufacturer.SelectedItem = manufacturerName;

MSDN documentation

Upvotes: 1

Pourya Zarei
Pourya Zarei

Reputation: 1

//use combobox and just set SelectedText
 ComboBox.SelectedText = manufacturerName;

Upvotes: 0

roozbeh S
roozbeh S

Reputation: 1124

I don't know what you exactly want to do here but you can assign your combobox to an item like this:

childEditProduct.cmbManufacturer.SelectedIndex = 
    childEditProduct.cmbManufacturer.Items.Cast<string>().ToList()
        .FindIndex(x => x == manufacturerName);

Upvotes: 0

Pancabiel
Pancabiel

Reputation: 133

You may want to use the ComboBox differently. Like using its DataSource and Id (int) instead of names (string). An example: how to bind a list to a combobox? (Winforms).

Upvotes: 0

Related Questions