Kate
Kate

Reputation: 372

ValueMember from ComboBox to int

i have valueMember in combobox and i need to save this value to integer... This is my code:

public class Benzinky
{
   public int B_cislo { get; set; }
   public string Benzinka { get; set; }
}

var lines = File.ReadAllLines(@"C:...\pokus.txt");
var data = lines.Select(l => l.Split());
List<Benzinky> allB = data.Where(arr => arr.Length >= 2
                               && arr[1].Trim().All(Char.IsDigit))
                           .Select(arr =>
                              new Benzinky
                              {
                                 Benzinka = arr[0].Trim(),
                                 B_cislo = int.Parse(arr[1].Trim())
                              })
                           .ToList();
var bindingSourceB = new BindingSource();
bindingSourceB.DataSource = allB;
comboBox1.DataSource = bindingSourceB;
comboBox1.ValueMember = "B_cislo";
comboBox1.DisplayMember = "Benzinka";

my txt:

Prague 3106
Berlin 3107
........

Have you any ideas?

Upvotes: 1

Views: 6697

Answers (2)

King King
King King

Reputation: 63317

ValueMember is just used to determine the value of your combobox SelectedValue. To get the valueMember part of your ComboBox item, you have to cast the underlying item (which is of type Benzinky in your example) to the correct type and get the desired value from some property, here is how it should be done if you know the underlying data type and valueMember beforehand:

int x = ((Benzinky) comboBox1.Items[index]).B_cislo;
//or using dynamic
dynamic item = comboBox1.Items[index];
int x = item.B_cislo;

However if you want something dynamic (that happen when the valueMember may change prorammatically at some time), you have to use Reflection like this:

object item = comboBox1.Items[index];
var x = (int) item.GetType().GetProperty(comboBox1.ValueMember)
                            .GetValue(item, null);

NOTE: However the Reflection approach is applicable only when the DataSource of your comboBox is not some class like DataTable, because a DataTable exposes its Column name as ValueMember not any properties of it, the underlying item will be a DataRowView and so in that case the reflection code will fail.

Upvotes: 3

Max
Max

Reputation: 13328

You should convert the valueMember of comboBox1 to an integer and put the result in Number. This can be done in multiple ways, you can use Convert.ToInt32(); But I would take a look at Int32.Parse() and Int32.TryParse()

Int32.Parse

Number = Int32.Parse(comboBox1.ValueMember);

Above code should do the trick, but you will run in to troubles when the string doesn't contain a value that can be parsed to an integer, an exception will be thrown.

You could use Int32.TryParse if you would like to get a bool value in return instead of an exception.

Int32.TryParse

int Number;
bool result = Int32.TryParse(comboBox1.ValueMember, out Number);
if (result)
{
   Console.WriteLine("Converted '{0}' to {1}.", comboBox1.ValueMember, Number);         
}
else
{
  //conversion failed
  //Int32.Parse, would throw a formatexception here.
}

Could you try following code:

comboBox1.DataSource = bindingSourceB;
comboBox1.ValueMember = "B_cislo";
comboBox1.DisplayMember = "Benzinka";
int Number;
if(Int32.TryParse(comboBox1.ValueMember, out Number))
{
  //Conversion succeeded
}
else
{
  //Conversion failed, you should send a message to the user
  //Or fill Number with a default value, your choice.
}

Sources:

MSDN Int32.Parse

MSDN Int32.TryParse

Upvotes: 3

Related Questions