Extracting specific string from combobox select

969 views Asked by At

I have a combobox for an item list that I populate using the following code:

List<string> comboboxItems = new List<string>();

foreach (var p in products)
{
   var x = p.Product;

   foreach (var pn in x)
   {
      comboboxItems.Add(pn.name + " :Qty " + pn.quantity_available
                         + " :Tax " + pn.default_tax_tier);                       
   }
}

cmbItems.DataSource = comboboxItems;

What should I do in order to get the value, pn.name only when the combobox item is selected?

Using WinForms.

3

There are 3 answers

4
King King On BEST ANSWER

You have to handle the event DataGridView.EditingControlShowing event, in there you can access the actual combobox and register the SelectedIndexChanged event handler like this:

//EditingControlShowing event handler for your dataGridView1
private void dataGridView1_EditingControlShowing(object sender,
                                   DataGridViewEditingControlShowingEventArgs e){
   if(dataGridView1.CurrentCell.OwningColumn == cmbItems){
     var combo = e.Control as ComboBox;
     combo.SelectedIndexChanged -= cmbItems_SelectedIndexChanged;
     combo.SelectedIndexChanged += cmbItems_SelectedIndexChanged;
   }   
}
private void cmbItems_SelectedIndexChanged(object sender, EventArgs e){
   var combo = sender as ComboBox;
   //Note that SelectedItem may be null
   var s = Convert.ToString(combo.SelectedItem);
   int i = s.IndexOf(" :Qty");
   var selectedName = i == -1 ? "" : s.Substring(0,i+1).TrimEnd();
   //other code ...
}
0
Franck On

you should create an item such as

public class CboItem
{
    public string Text { get; set; }
    public object Value { get; set; }

    public override string ToString()
    {
        return Text;
    }
}

then you can create easily using something like

CboItem item = new CboItem();
item.Text = "My Item";
item.Value = "Anything";

in the Value you can store your var pn whatever it is. then you can retrieve like so :

((CboItem)comboBox1.SelectedItem).Value;

You will need to cast the result above to the proper type you stored inside as the Value is of type object.

0
Karthik J On

We can also regular expressions to extract data from string.

Create string variable in the below format

string inputPattern = "{0} :Qty {1} :Tax {2}";

While inserting the data into combo box,

 comboboxItems.Add(string.Format(inputPattern, p.Name, p.Quantity_Available, p.Tax));

After you added it, to extract the strings we can use Regex, code snippet below.

string extractPattern = "(?<Name>.*) :Qty (?<Qty>.*) :Tax (?<Tax>.*)";
        foreach (var item in (comboBox1.DataSource as List<string>))
        {
            var matches = Regex.Match(item, extractPattern);
            if (matches.Groups["Name"].Success)
            {
                MessageBox.Show(matches.Groups["Name"].Value);
            }
            if (matches.Groups["Qty"].Success)
            {
                MessageBox.Show(matches.Groups["Qty"].Value);
            }
            if (matches.Groups["Tax"].Success)
            {
                MessageBox.Show(matches.Groups["Tax"].Value);
            }
        }