本文介绍了如何从SqlServer获取值并将其填充到其corrspond ComboBox中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的数据库中我有一个表'EmpRole',其中存储员工的各种角色,如CEO,开发人员等。我在我的项目注册表中使用此角色。这些角色显示在组合框中。在注册时,这工作正常,但是当我要在表单上查看注册用户的配置文件时,不会选择相应的组合框值,而是默认选择组合框的第一个值。

我希望在配置文件预览时在组合框中选择适当的emprole值





in my DB i have a table 'EmpRole' where various roles of an employee are stored like CEO, Developer etc. And i am using this roles in my project registration form. these roles are displaying in a combobox. At time of registration this works fine, but when i m going to view the profile of registered users on the form the appropriate combobox value not get selected instead first value of the combobox selected by default.
I want that appropriate value of emprole get selected in combobox at time of profile preview


using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCN"].ConnectionString))
            {
                btnRegister.Text = "Update";

                SqlCommand cmd = new SqlCommand("Usp_Register", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@Mode", "ViewProfile");
                con.Open();

                SqlDataAdapter da = new SqlDataAdapter();

                DataTable dt = new DataTable();
                da.SelectCommand = cmd;
                da.Fill(dt);
                if (dt != null && dt.Rows.Count > 0)
                {


                    txtName.Text = dt.Rows[0]["EmpName"].ToString();
                    txtPassword.Text = dt.Rows[0]["Password"].ToString();
                    //txtPassword.Enabled = false;
                    txtUserName.Text = dt.Rows[0]["UserName"].ToString();

                    txtDesignation.Text = dt.Rows[0]["EmpDesignation"].ToString();
                    txtMobileNumber.Text = dt.Rows[0]["Empnumber"].ToString();
                    txtEmailId.Text = dt.Rows[0]["EmpEmail"].ToString();
                    bindRole();
                    comboboxRole.SelectedValue = dt.Rows[0]["EmpRole"].ToString();
                    dateTimePickerJoinDate.Value = Convert.ToDateTime(dt.Rows[0]["JoinDate"].ToString());
                    dateTimePickerDOB.Value = Convert.ToDateTime(dt.Rows[0]["DOB"].ToString());
                    if (dt.Rows[0]["Gender"].ToString() == "Male")
                    {
                        radioButtonMale.Checked = true;

                    }
                    else if (dt.Rows[0]["Gender"].ToString() == "Female")
                    {
                        radioButtonFemale.Checked = true;
                    }
                }

                else
                {
                    MessageBox.Show("No Data in dt");
                }

            }







并且有bindRole ()方法








and there is the bindRole() method


public void bindRole()
        {
            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCN"].ConnectionString))
            {

                DataSet ds = new DataSet();

                SqlDataAdapter da = new SqlDataAdapter("Select * from EmpRole Order by RoleName", con);
                con.Open();
                da.Fill(ds, "EmpRole");
                comboboxRole.DisplayMember = "RoleName";
                comboboxRole.ValueMember = "RoleId";
                comboboxRole.DataSource = ds.Tables["EmpRole"];
            }

推荐答案


这篇关于如何从SqlServer获取值并将其填充到其corrspond ComboBox中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 03:17