gpt4 book ai didi

c# - 如何将组合框的数据表示为整数

转载 作者:太空宇宙 更新时间:2023-11-03 10:58:48 26 4
gpt4 key购买 nike

在我的项目中,有一个组合框“status”,其中包含三个值 active、in active 和 working。根据要求,所选值应表示为整数,因此我编写了代码,以便存储所选值的索引。现在的问题是,当我尝试在更新状态时从组合框中选择值时,它应该在 ListView 中显示为值。如果我选择 0,它应该显示为事件状态,1 为非事件状态,2 为工作状态。到目前为止,我用来获取值的代码如下,请帮助我获取该整数的值。

private void btnUpdateSupport_Click(object sender, EventArgs e)
{
SqlConnection con = Helper.getconnection();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
string SupportName = txtSupportName.Text;
string SupportDesignation = txtSupportDesignation.Text;
//string SupportStatus = txtSupportStatus.Text;
string SupportStatus = cbSupportStatus.SelectedItem.ToString();
//string SupportStatus = SqlCommand();
int i = 0;
string s = "Active";
// string result = int.TryParse(s, out i);

if (cbSupportStatus.SelectedItem != null)
{
int x = int.Parse(cbSupportStatus.SelectedItem.ToString());
}
else
{ //Value is null }

cmd.CommandText = "Update Status1 set Designation='" + SupportDesignation + "', Status='" + SupportStatus + "' where Name= '" + SupportName + "' ";
MessageBox.Show(cmd.CommandText);
con.Open();
cmd.ExecuteNonQuery();
con.Close();

}
}

最佳答案

根据您的评论,组合框只有数字 0、1 和 2

我假设这些数字从一种形式存储到数据库(?),然后以另一种形式检索到您想要的地方:

  • 0 = 活跃
  • 1 = 不活跃
  • 2 = 工作

显示在列表框中

为什么不这样做:

string listVal = "";
if(storedVal == 0)
{
listVal = "Active";
}
else if(storedVal == 1)
{
listVal = "Inactive";
}
else if(storedVal == 2)
{
listVal = "Working";
}

if(listVal != "")
{
// add to listbox
}

编辑或者不是将数字存储在数据库中,而是存储它的含义,然后只为列表框检索该文本

关于c# - 如何将组合框的数据表示为整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18464086/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com