简体   繁体   English

获取组合框的值到文本框

[英]getting value of combobox to textbox

using query i have called two columns value from database into one column.使用查询,我将数据库中的两列值调用到一列中。 the point is now i want to select a value form combobox and put one column value into textbox.现在的重点是我想选择一个值表单组合框并将一列值放入文本框中。

eg例如

two column values from database into combobox below从数据库到下面的组合框的两列值

10001 haider <------ when i select this index i want only haider to be viewed into the textbox 10001 Haider <------ 当我选择此索引时,我只想在文本框中查看海德尔

10002 fahad 10002 法赫

10003 aitazaz 10003 艾塔扎兹

the snippet which i have used for calling the two colums value from database is:我用来从数据库中调用两列值的代码片段是:

public void account()
        {
            con.Open();
            cmd.Connection = con;
            cmd.CommandText = "SELECT acc_no, acc_name FROM accounts_record";
            MySqlDataAdapter adpt = new MySqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            adpt.Fill(ds);

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                cbacc.Items.Add(ds.Tables[0].Rows[i][0] + "   " + ds.Tables[0].Rows[i][1]);
            }

            con.Close();
        }

You should be adding values and text to the combobox separately.您应该分别向组合框添加值和文本。 Here's an example ComboBox: Adding Text and Value to an Item (no Binding Source) .这是一个ComboBox示例:向项目添加文本和值(无绑定源)

If you have to display the id in the text you have to do some parsing before putting the selected text into the textbox.如果必须在文本中显示 id,则必须在将所选文本放入文本框之前进行一些解析。

If you are able to get the 2 value text of the combo box on selection change then you can split it with space (" ") character and take the 2nd string and put it in textbox.如果您能够在选择更改时获得组合框的 2 值文本,那么您可以用空格 (" ") 字符将其拆分并取第二个字符串并将其放入文本框中。

For example例如

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string[] splitedStr = comboBox1.SelectedText.Split(' ');
        textBox1.Text = splitedStr[1];
    }

Use ComboBox.SelectedIndexChanged event of the combobox to populate the textbox accordingly.使用组合框的 ComboBox.SelectedIndexChanged 事件相应地填充文本框。 use http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectedindexchanged(v=vs.110).aspx使用http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectedindexchanged(v=vs.110).aspx

You should use the code as below您应该使用以下代码

   private void Form1_Load(object sender, EventArgs e)
    {
            string conString = "Data Source=\\SQLEXPRESS;Initial Catalog=Test;Integrated                 Security=True";
            SqlConnection con = new SqlConnection(conString);
            SqlCommand cmd = new SqlCommand();
            con.Open();
            cmd.Connection = con;
            cmd.CommandText = "SELECT acc_no +'-' + acc_name as AccNoWithName , acc_no as ActNo FROM accounts_record";
            SqlDataAdapter adpt = new SqlDataAdapter(cmd);
            DataSet dsn = new DataSet();
            adpt.Fill(dsn);
            con.Close();

            comboBox1.DisplayMember = "AccNoWithName";
            comboBox1.ValueMember = "ActNo";
            comboBox1.DataSource = dsn.Tables[0];
   }


private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
            textBox1.Text = comboBox1.SelectedValue.ToString();
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM