[英]Best method of getting the string value from the combobox to be used in database comparison
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.
他们的信息在组合框中显示为1-info。 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.
我已经能够找出将其转换回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 添加了combox填充方法
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);
}
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. 现在,如果结果能够解析该值而不抛出异常,则
result
为true / false。
如果您不将ComboBox项目的ID字段用于其他目的,则最好在其中存储数字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.