将从组合框中选择的整数表示为文本。 [英] Representing the integer selected from the combo box as text.

查看:82
本文介绍了将从组合框中选择的整数表示为文本。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,有一个组合框status,其中三个值处于活动状态,处于活动状态和工作状态。根据要求,所选值应表示为整数,因此我编写了代码,以便存储所选值的索引。现在问题是,当我尝试在更新状态时从组合框中选择值时,它应该在列表视图中显示为值。如果我选择0,它应该显示为活动,1表示处于活动状态,2表示工作。到目前为止我用来获取值的代码如下,请帮助我获取整数的值。代码是:



In my project, there is a combo box "status" with the three values active, in active and working. As per the requirement the selected value should represent as a integer, so I wrote code so that the index of the selected value will store. Now the problem is, when I tried to select the value from the combo box while updating the status, it should be shown as value in the listview. That if I select 0 it should show active, 1 as in-active and 2 as working. The code so far I used to get the values is below, please help me getting the values to that integers. The code is:

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();

            }
        }

推荐答案

你用 SelectedItem ,原则上是错误的。您不必在组合框或列表框中存储字符串。您可以存储任何对象。这样,您将避免将项目文本解析为整数或其他任何内容的不可支持,低效和不可靠。相反,你只需将项解析为整数(但你应该确定你真的存储了整数)。



在这种方法中,它经常为存储实例付出代价一些 struct class ,包含该功能所需的所有信息。唯一的问题是:如果存储了您选择的某些对象,将在UI中显示的内容。答案很简单:无论 ToString()(不带参数)都返回。因此,解决方案也很简单:对于您的元素类型,覆盖 System.Object.ToString()它将显示您需要的方式。这是一种简单而强大的技术。



-SA
You are doing a wrong thing with SelectedItem, wrong in principle. You don't have to store strings in a combo box or a list box. You can store any objects. That way, you will avoid unsupportable, inefficient and unreliable parsing of the item's text to integer or anything else. Instead, you would simply parse the item as integer (but you should be sure that you really store integers).

In this approach, it often pays off to store instances of some struct or class with all the information you need to the functionality. The only problem is: what will be shown in the UI if you store some objects of your choice. The answer is simple: whatever ToString() (without parameters) returns. So, the solution is also simple: for your element type, override System.Object.ToString() the way it would show what you need. This is a simple and robust technique.

—SA


这篇关于将从组合框中选择的整数表示为文本。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆