Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用SQL Server数据库中的数据自动填充组合列表_C#_Sql_Combobox - Fatal编程技术网

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

C# 使用SQL Server数据库中的数据自动填充组合列表,c#,sql,combobox,C#,Sql,Combobox,请任何人帮忙,我正在尝试将数据自动填充到我的组合框中,无需按下任何按钮,而是通过下拉控件 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { if (comboBox1.AllowDrop == false) { SqlConnection conn = new SqlConnection("Data Source=localhost; database=KnowledgeEs

请任何人帮忙,我正在尝试将数据自动填充到我的组合框中,无需按下任何按钮,而是通过下拉控件

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
   {
   }
}
private void组合框1\u SelectedIndexChanged(对象发送方,事件参数e)
{
if(comboBox1.AllowDrop==false)
{
SqlConnection conn=newsqlconnection(“数据源=localhost;数据库=knowledgesentials;可信连接=yes;连接超时=30”);
SqlDataAdapter=新的SqlDataAdapter(“从PROBLEMT中选择问题”,conn);
DataTable dt=新的DataTable();
数据集ds=新数据集();
SqlDataAdapter ad=新的SqlDataAdapter();
ad.SelectCommand=newSQLCommand(“从问题中选择问题”,康涅狄格州);
广告填充(ds,“问题”);
dataGridView1.DataSource=dt;
//将数据与控件绑定
BindingSource bs=新的BindingSource();
bs.DataSource=ds;
bs.DataMember=“问题”;
DataGridView dvg=新建DataGridView();
this.Controls.Add(dvg);
dvg.DataSource=bs;
对于(int i=0;i
您缺少了一个
}
,我几乎不相信SelectedIndexChanged是填充组合框的最佳位置。由于您显然正在学习,请尝试将代码放在按钮上,一旦它通过单击按钮工作正常,您就可以寻找更好的位置,例如表单加载


还有,到底是什么问题?它是否给出错误?

首先创建用于获取数据的方法,然后像这样加载到组合框中

    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()); 
        } 

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

             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);
     }

我有一个工作按钮,当我点击它时,它可以完美地工作,然后它将数据填充到组合框中
             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);
     }