ComboBox Windows窗体

时间:2014-03-23 14:20:05

标签: .net winforms

    private void button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection();
        SqlCommand cmd = new SqlCommand();
        SqlDataAdapter da = new SqlDataAdapter();

        con.ConnectionString = @"Data Source=.;Initial Catalog=EmployeeDetails1;Integrated Security=True";
        cmd.CommandText = "insert into EmployeeDetails(Name,Gender,JobPosition,JobStatus)values(@name,@gender,@jobposition,@jobstatus)";
        cmd.Connection = con;
        con.Open();

        SqlParameter pa1 = new SqlParameter("@name", DbType.String);
        pa1.Value = txtName.Text;
        cmd.Parameters.Add(pa1);


        SqlParameter pa2 = new SqlParameter("@gender", DbType.String);

        if (rdiobtnFemale.Checked)
        {
            pa2.Value = rdiobtnFemale.Text;
        }
        else
        {
            pa2.Value = rdiobtnMale.Text;
        }
        cmd.Parameters.Add(pa2);


        SqlParameter pa3 = new SqlParameter("@JobPosition", DbType.String);

        if (chkboxManager.Checked)
        {
            pa3.Value = chkboxManager.Text;
        }
        if (chkboxExecutive.Checked)
        {
            pa3.Value = pa3.Value + " " + chkboxExecutive.Text;
        }
        cmd.Parameters.Add(pa3);


        SqlParameter pa4 = new SqlParameter("@JobStatus", DbType.String);
        pa4.Value = cmbboxJobStatus.SelectedItem.ToString();
        cmd.Parameters.Add(pa4);

        da.InsertCommand = cmd;
        cmd.ExecuteNonQuery();
        con.Close();

    }

    private void btnShow_Click(object sender, EventArgs e)
    {
        MessageBox.Show("Enter Name To Get Results");
        SqlConnection con = new SqlConnection();
        SqlCommand cmd = new SqlCommand();
        SqlDataAdapter da = new SqlDataAdapter();
        DataTable dt = new DataTable();

        con.ConnectionString = @"Data Source=.;Initial Catalog=EmployeeDetails1;Integrated Security=True";
        cmd.CommandText = "select * from EmployeeDetails where Name=@name";
        cmd.Connection = con;
        con.Open();

        SqlParameter pa1 = new SqlParameter("@name", DbType.String);
        pa1.Value = txtName.Text;
        cmd.Parameters.Add(pa1);

        da.SelectCommand = cmd;
        da.Fill(dt);

        chkboxManager.Checked = false;
        chkboxExecutive.Checked = false;
        rdiobtnFemale.Checked = false;
        rdiobtnMale.Checked = false;

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            string str, str1, str2, str3, str4, str5, str6;
            str = dt.Rows[i][1].ToString();
            str1 = chkboxManager.Text;
            str2 = chkboxExecutive.Text;
            str3 = dt.Rows[i][3].ToString();
            str4 = rdiobtnFemale.Text;
            str5 = dt.Rows[i][2].ToString();


            //checkbox
            if (str == str1)
            {

                chkboxManager.Checked = true;
            }
            if (str == str2)
            {
                chkboxExecutive.Checked = true;
            }
            if (str == str1 + " " + str2)
            {
                chkboxExecutive.Checked = true;
                chkboxManager.Checked = true;
            }

            //radio button
            if (str3 == str4)
            {
                rdiobtnFemale.Checked = true;
            }
            else
            {
                rdiobtnMale.Checked = true;
            }

            //combobox

            str6 = cmbboxJobStatus.Items[i].ToString();
            if (str5 == str6)
            {

                cmbboxJobStatus.Text = str6;
            }


        }


    }
}

}

  

有诸如&#34; Permanent&#34;,&#34; OnContract&#34;等值。 ComboBox中提到的等等。我已经通过插入按钮单击将这些值插入到数据库中。单击show按钮,我希望程序搜索数据库表并获取我在文本框中提到的名称的记录。组合框的详细信息也应显示为数据库中提及的内容。

1 个答案:

答案 0 :(得分:0)

看起来像杀死蚊子的导弹: - ]

无论如何,您只需要检查您的组合框是否包含从数据库中检索的项目,然后使用SelectedText属性进行设置。您需要替换下面的代码

str6 = cmbboxJobStatus.Items[i].ToString();
if (str5 == str6)
{

    cmbboxJobStatus.Text = str6;
}

有了这个

if (cmbboxJobStatus.Items.Contains(str5))
{
    cmbboxJobStatus.SelectedText = str5;
}