1

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

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
   {
   }
}
4

3 回答 3

0

你错过了一个},我几乎不相信 SelectedIndexChanged 是填充组合框的最佳位置。由于显然您正在学习,因此请尝试将代码放在按钮上,一旦单击按钮即可正常工作,您可以为其寻找更好的位置,例如表单加载。

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

于 2012-07-24T09:15:28.360 回答
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();

 }
}
于 2012-07-24T09:20:26.947 回答
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);
     }
于 2012-07-31T10:43:27.653 回答