如何根据文本框中选择的值填充其他文本框?

时间:2016-05-10 05:10:21

标签: c# autocomplete

textbox1的代码使用TextChanged事件的自动完成功能,并在Person Name中显示来自数据库的textbox1作为建议项目。现在,如果用户从textbox1中的建议项目中选择特定名称,我希望根据textbox2的值从数据库中自动填充textbox3textbox1。我该怎么做?

textbox1代码:

private void textBox1_TextChanged(object sender, EventArgs e)
{
    AutoCompleteStringCollection namesCollection = new AutoCompleteStringCollection();
    SqlConnection con = new SqlConnection(@"***my connection string***");
    con.Open();
    SqlCommand cmnd = con.CreateCommand();
    cmnd.CommandType = CommandType.Text;
    cmnd.CommandText = "SELECT * FROM tblTicketDetail";
    SqlDataReader dReader;
    dReader = cmnd.ExecuteReader();

    if (dReader.Read())
    {
        while (dReader.Read())
            namesCollection.Add(dReader["ContactPerson"].ToString());
    }
    else
    {
        MessageBox.Show("Data not found");
    }
    dReader.Close();

    textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
    textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
    textBox1.AutoCompleteCustomSource = namesCollection;
}

1 个答案:

答案 0 :(得分:0)

首先将你的代码转换到try catch bloc中,之后,在try bloc的末尾,从textbox1获取文本,关闭上一个reader,然后根据textbox1文本打开另一个reader,填充textbox2结果并再次关闭第二个阅读器,然后对textbox3执行相同的操作,

相关问题