C# - 在 Windows 窗体应用程序中获取 SelectedItem 的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10318693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# - getting value of SelectedItem in Windows Forms application
提问by KalC
I have a simple Windows Forms application (with an Access database) with a combobox (cmbStores) that is populated in the most simple way imaginable.
我有一个简单的 Windows 窗体应用程序(带有 Access 数据库),带有一个组合框 (cmbStores),它以可以想象的最简单的方式填充。
Problem: I am unable to get the value of the selected item.
问题:我无法获取所选项目的值。
Here is how I am populating this combobox:
这是我填充此组合框的方式:
// Variable declaration
string strQueryStores = "SELECT StoreNumber FROM tblStoresAndRegion ORDER BY StoreNumber";
string strConnectionString = UtilityClass.GetConnectionString();
OleDbConnection connStores;
OleDbDataReader readerStores = null;
connStores = new OleDbConnection(strConnectionString);
try
{
connStores.Open();
OleDbCommand sqlGetStores = new OleDbCommand(strQueryStores, connStores);
cmbStore.Items.Clear();
cmbStore.Items.Add("All");
if (connStores != null)
{
readerStores = sqlGetStores.ExecuteReader();
if (readerStores.HasRows)
{
while (readerStores.Read())
{
cmbStore.Items.Add (Convert.ToInt32(readerStores["StoreNumber"]));
}
}
}
cmbStore.SelectedIndex = 0;
}
catch (OleDbException oledblEX)
{
MessageBox.Show(oledblEX.Message);
}
finally
{
if (readerStores != null)
readerStores.Close();
if (connStores != null)
connStores.Close();
}
This is how I am trying to get the value of the selected item.
这就是我试图获取所选项目值的方式。
int nStoreNumber = Convert.ToInt32(cmbABSM.SelectedItem);
采纳答案by TruthSeeker
I know I am a little late, but this works well:
我知道我有点晚了,但这很有效:
int? nStoreNumber = cmbABSM.SelectedValue as int?;
if (nStoreNumber==null)
return;
回答by Khan
Try using SelectedValueif ValueMemberis set for the combobox, otherwise default to the Textproperty:
尝试使用SelectedValueifValueMember为组合框设置,否则默认为Text属性:
//If ValueMember is set
int nStoreNumber = Convert.ToInt32(cmbABSM.SelectedValue);
//Otherwise
int nStoreNumber = Convert.ToInt32(cmbABSM.Text);
Either way I would recommend you make sure the value of what is selected is a valid int.
无论哪种方式,我都会建议您确保所选内容的值是有效的int.
int nStoreNumber;
if (!int.TryParse(cmbABSM.SelectedValue, out nStoreNumber))
{
//This is not a valid number. Notify the user.
}
回答by csteinmueller
does
做
Int32.Parse(box.SelectedItem.ToString());
work for you?
为你工作?
回答by NGRhodes
You could use SelectedItem.Value or SelectedValue, the practical difference is in what they return when there is no selection.
您可以使用 SelectedItem.Value 或 SelectedValue,实际区别在于它们在没有选择时返回的内容。
SelectedItem.Value returns the value, will return null if there is no selected item.
SelectedItem.Value 返回值,如果没有选中的项,则返回 null。
SelectedValue also returns the value, but will return an empty string if there is no selected item
SelectedValue 也返回值,但如果没有被选中的项目,将返回一个空字符串
Further reading:
进一步阅读:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.selecteditem.aspx
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.selecteditem.aspx
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.selectedvalue.aspx
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.selectedvalue.aspx

