我习惯使用Java,那里有大量的例子.由于各种原因,我不得不切换到C#并尝试在SharpDevelop中执行以下操作:
// Form has a menu containing a combobox added via SharpDevelop's GUI // --- Variables languages = new string[2]; languages[0] = "English"; languages[1] = "German"; DataSet myDataSet = new DataSet(); // --- Preparation DataTable lTable = new DataTable("Lang"); DataColumn lName = new DataColumn("Language", typeof(string)); lTable.Columns.Add( lName ); for( int i=0; i人们会假设在下拉列表中看到一些值,但它是空的.请告诉我我做错了什么;(
编辑:mnuActionLanguage.ComboBox.DataBind()是我也在网上找到的,但它在我的情况下不起作用.
解
mnuActionLanguage.ComboBox.BindingContext = this.BindingContext;最后解决了问题!
1> BlackWasp..:您需要设置ToolStripComboBox.ComboBox的绑定上下文.
这是我刚刚使用Visual Studio重新创建的代码的略微修改版本.在我的例子中,菜单项组合框被称为toolStripComboBox1.请注意设置绑定上下文的最后一行代码.
我注意到,如果组合在工具栏的可见区域中,则绑定在没有这个的情况下工作,但在下拉列表时则不然.你有同样的问题吗?
如果你不能让这个工作,请通过我的联系页面给我发一条线,然后我会发给你这个项目.您将无法使用SharpDevelop加载它,但将使用C#Express加载它.
var languages = new string[2]; languages[0] = "English"; languages[1] = "German"; DataSet myDataSet = new DataSet(); // --- Preparation DataTable lTable = new DataTable("Lang"); DataColumn lName = new DataColumn("Language", typeof(string)); lTable.Columns.Add(lName); for (int i = 0; i < languages.Length; i++) { DataRow lLang = lTable.NewRow(); lLang["Language"] = languages[i]; lTable.Rows.Add(lLang); } myDataSet.Tables.Add(lTable); toolStripComboBox1.ComboBox.DataSource = myDataSet.Tables["Lang"].DefaultView; toolStripComboBox1.ComboBox.DisplayMember = "Language"; toolStripComboBox1.ComboBox.BindingContext = this.BindingContext;
2> 小智..:string strConn = "Data Source=SEZSW08;Initial Catalog=Nidhi;Integrated Security=True"; SqlConnection Con = new SqlConnection(strConn); Con.Open(); string strCmd = "select companyName from companyinfo where CompanyName='" + cmbCompName.SelectedValue + "';"; SqlDataAdapter da = new SqlDataAdapter(strCmd, Con); DataSet ds = new DataSet(); Con.Close(); da.Fill(ds); cmbCompName.DataSource = ds; cmbCompName.DisplayMember = "CompanyName"; cmbCompName.ValueMember = "CompanyName"; //cmbCompName.DataBind(); cmbCompName.Enabled = true;