Reputation: 65
I have a combobox that is populated on the form load event. Of course the values are string format. They info is displayed 1-info in the combobox. I want to take that first digit and compare it to a value in my database. Based off which value it finds then populates the fields on the form. Here is what I have so far. I have been able to figure out as far as converting it back to int 32.
if (cmboBoxPreviousVersion.SelectedItem != null)
{
string[] s = cmboBoxPreviousVersion.Items[cmboBoxPreviousVersion.SelectedIndex].ToString().Split(' ');
int id = Convert.ToInt32(s[0]);
Item.FormatID = data.FormatID;
Item.FormatName = data.FormatName;
Item.FormatDescription = data.FormatDescription;
Item.StockID = data.StockID;
Item.PrintPlantCode = (bool)data.PrintPlantCode;
Item.PrintWeight = (bool)data.PrintWeight;
Item.PrintPrice = (bool)data.PrintPrice;
rChkBoxPlantCode.Checked = Item.PrintPlantCode;
rChkBoxPrintPrice.Checked = Item.PrintPrice;
rChkBoxWeight.Checked = Item.PrintWeight;
cmboBoxStock.Items.Add(Item.StockID);
rTxtBoxDescription.Text = Item.FormatDescription;
}
rChkBoxPlantCode.Enabled = false;
rChkBoxPrintPrice.Enabled = false;
rChkBoxWeight.Enabled = false;
Any suggestions? Thank you before hand. If you need any other info or clarification let me know!
Added combox fill method
try
{
List<PreviousVersionData> listID = PreviousVersionData.getDatabase();
if (listID != null)
{
foreach (PreviousVersionData l in listID)
{
cmboBoxPreviousVersion.Items.Add(string.Format("{0} - {1}", l.FormatID, l.FormatName));
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Upvotes: 1
Views: 305
Reputation: 3036
If you dont use ComboBox item's ID field for other purposes then better store numbers in there.
Upvotes: 0
Reputation: 17380
I would use:
int id;
bool result = Int32.TryParse(s[0], out id);
Now result
has true/false if it was able to parse the value, without throwing exceptions.
Upvotes: 1