0

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.

decPL
  • 5,384
  • 1
  • 26
  • 36
Kinyanjui Kamau
  • 1,890
  • 10
  • 55
  • 95

3 Answers3

1

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 ...
}
King King
  • 61,710
  • 16
  • 105
  • 130
  • @KinyanjuiKamau see the updated code right in the second method `cmbItems_SelectedIndexChanged` – King King Dec 04 '13 at 12:58
  • 1
    This is just what I needed. Just added .Trim() to the end of your last statement and works perfectly. +1 – Kinyanjui Kamau Dec 04 '13 at 13:10
  • @EmmadKareem it's from your tag **`datagridcomboboxcolumn`**, so I guessed your `cmbItems` is a `DataGridViewComboBoxColumn`, not a `ComboBox`, however if it's just a combobox, the code is much simpler to attach the `SelectedIndexChanged` event handler. – King King Dec 04 '13 at 13:19
  • Yes, cmbItems is a DataGridViewComboBoxColumn. – Kinyanjui Kamau Dec 04 '13 at 13:23
0

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.

Franck
  • 4,438
  • 1
  • 28
  • 55
0

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);
            }
        }
Karthik J
  • 3
  • 2