使用SQL Server数据库中的数据自动填充组合列表

时间:2012-07-24 08:51:03

标签: c# sql combobox

任何人都可以帮忙,我试图将数据自动填充到我的组合框而不必按任何按钮,但是通过下拉控件.....

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
   if (comboBox1.AllowDrop == false)
   {
      SqlConnection conn = new SqlConnection("Data Source=localhost; database=KnowledgeEssentials;Trusted_Connection=yes;connection timeout=30");
      SqlDataAdapter adapter = new SqlDataAdapter("SELECT Problem FROM PROBLEMT", conn);

      DataTable dt = new DataTable();
      DataSet ds = new DataSet();

      SqlDataAdapter ad = new SqlDataAdapter();
      ad.SelectCommand = new SqlCommand("SELECT Problem FROM PROBLEMT", conn);
      ad.Fill(ds, "Problem");

      dataGridView1.DataSource = dt;
      //Biding the data with the control
      BindingSource bs = new BindingSource();
      bs.DataSource = ds;
      bs.DataMember = "Problem";

      DataGridView dvg = new DataGridView();
      this.Controls.Add(dvg);
      dvg.DataSource = bs;

      for (int i = 0; i < dt.Rows.Count; i++)
      {
          comboBox1.Items.Add(dt.Rows[i]["Problem"]);
      }
   }
   else
   {
   }
}

3 个答案:

答案 0 :(得分:0)

你错过了},我几乎不相信SelectedIndexChanged是填充组合框的最佳位置。因为很明显你正在学习,所以尝试将代码放在一个按钮上,一旦单击按钮就可以正常工作,你可以找到一个更好的地方,例如表单加载。

究竟是什么问题?它会出错吗?

答案 1 :(得分:0)

首先创建用于获取数据并加载到组合框中的方法,如下所示

    protected void LoadCombo()
    {

           SqlConnection conn = new SqlConnection("Data Source=localhost;database=KnowledgeEssentials;Trusted_Connection=yes;connection timeout=30"); 
            SqlDataAdapter adapter = new SqlDataAdapter("SELECT Problem FROM PROBLEMT", conn); 
            DataSet ds = new DataSet(); 
            SqlDataAdapter ad = new SqlDataAdapter(); 
            ad.SelectCommand = new SqlCommand("SELECT Problem FROM PROBLEMT", conn); 
            ad.Fill(ds, "Problem"); 
            dataGridView1.DataSource = ds; 
            dataGridView1.DataBind(); 
        for (int X = 0; X <= ds.Tables[0].Rows.Count - 1; X++)
        { 
            comboBox1.Items.Add(ds.Tables[0].Rows[X]["Problem"].ToString()); 
        } 

}

现在在page_load上调用此方法

protected void Page_Load()
{

if(ispostback)
 {

}
else
{
 LoadCombo();

 }
}

答案 2 :(得分:0)

回答有关如何自动填充组合框的问题:

             private void Form3_Load(object sender, EventArgs e)
              {
                  using (SqlConnection sc = new SqlConnection())
                   {
                    sc.ConnectionString= "database=KnowledgeEssentials;Trusted_Connection=yes;connection timeout=30";
                    sc.Open();
                    using (SqlDataAdapter sda = new SqlDataAdapter())
                     {
                       DataTable data = new DataTable();
                       sda.SelectCommand = new SqlCommand("SELECT ID, TypeProblem FROM eL_Section", sc);
                       sda.Fill(data);

                       comboBox1.ValueMember = "ID";
                       comboBox1.DisplayMember = "ID";
                       comboBox1.DataSource = data;

                       comboBox2.ValueMember = "TypeProblem";
                       comboBox2.DisplayMember = "TypeProblem";
                       comboBox2.DataSource = data;


                     }


             }

            using (SqlConnection cn = new SqlConnection())
              {
            cn.ConnectionString = "database=KnowledgeEssentials;Trusted_Connection=yes;connection timeout=30";
            cn.Open();
                using (SqlDataAdapter da = new SqlDataAdapter())
                  {
                    DataTable dat = new DataTable();
                    da.SelectCommand = new SqlCommand("SELECT UserName FROM UserT", cn);
                    da.Fill(dat);



                    comboBox3.ValueMember = "UserName";
                    comboBox3.DisplayMember = "UserName";
                    comboBox3.DataSource = dat;

                  }
              }


        // TODO: This line of code loads data into the 'knowledgeEssentialsDataSet.ProblemT' table. You can move, or remove it, as needed.
        this.problemTTableAdapter.Fill(this.knowledgeEssentialsDataSet.ProblemT);
     }