组合框选定项目更改时的texbox数量

时间:2018-01-27 14:05:02

标签: c#

大家好我试图在使用带有usercontrol表单的Microsoft sql server从组合框下拉列表中选择项目时自动填充一些文本框。我已经编写了下面的代码,但是我收到了combox下拉列表中的项目,但是当我在组合框下拉列表中选择一个项目时,文本框中没有任何内容。这些值位于名为竞争对手的表中。列是Institution,Region,FirstName,LastName,当我选择combox下拉列表时,我希望值显示在相应的文本框中。我寻求你的帮助来解决这个问题。提前谢谢

    private void RabbitCare_Load(object sender, EventArgs e)
    {
        CBoxParishDdlist.Items.Clear();
        SqlConnection connect = new SqlConnection(@"Data Source=ITSPECIALIST\SQLPROJECTS;Initial Catalog=EXPRORESULTS;Integrated Security=True ");
        connect.Open();

        SqlCommand cmd = connect.CreateCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "Select ParishName FROM Competitors WHERE CompetitiveEventName = 'Care and Management of Bees'";
        cmd.ExecuteNonQuery();
        DataTable dat = new DataTable();
        SqlDataAdapter SDA = new SqlDataAdapter(cmd);
        SDA.Fill(dat);
        foreach (DataRow DAR in dat.Rows)
        {
            CBoxParishDdlist.Items.Add(DAR["ParishName"].ToString());
        }
        connect.Close();
    }

    private void CBoxParishDdlist_SelectedIndexChanged(object sender, EventArgs e)
        {

            SqlConnection connect = new SqlConnection(@"Data Source=ITSPECIALIST\SQLPROJECTS;Initial Catalog=EXPRORESULTS;Integrated Security=True ");
            connect.Open();
            cmd = new SqlCommand("SELECT * FROM  Competitors WHERE ParishName = '" + CBoxParishDdlist.Text + "'", connect);



            cmd.ExecuteNonQuery();
            SqlDataReader dr;
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {

                string Institution = (string)dr["Institution"].ToString();
                TxtBoxInstitution.Text = Institution;

                string Region = (string)dr["Region"].ToString();
                TxtBoxRegion.Text = Region;

                string FirstName = (string)dr["FirstName"].ToString();
                TxtBoxFname.Text = FirstName;

                string LastName = (string)dr["LastName"].ToString();
                TxtBoxLname.Text = LastName;

            }
            connect.Close();

1 个答案:

答案 0 :(得分:0)

我认为这可以解决您的问题。

TxtBoxInstitution.Text = comboBoxName.SelectedItem.ToString();