为什么我无法从组合框中选择值? [英] Why I can't select the values from the combobox?

查看:116
本文介绍了为什么我无法从组合框中选择值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个winforms的项目,在我的RegisterStudent表单中,我有一个组合框,其中包含教育专栏。基于组合框的选择,将出现一系列复选框。问题是,当我想从中选择一个值时组合框,我不能。而不是默认值的课程出现。这是我的方法将数据加载到组合框:

 private void comboBox1_SelectedIndexChanged(object sender,EventArgs e)
{
LoadEducation();
}
private void LoadEducation()
{
using(SqlConnection conn = new SqlConnection(connstr))
{
try
{
string query =从AvailableCourses中选择教育;

da = new SqlDataAdapter(query,conn);
conn.Open();
ds = new DataSet();
da.Fill(ds,AvailableCourses);
comboBoxEducation.DisplayMember =education;
comboBoxEducation.ValueMember =education;
comboBoxEducation.DataSource = ds.Tables [AvailableCourses];
}
catch(Exception ex)
{
//将异常信息写入日志或其他任何内容
MessageBox.Show(出现错误!);
}
}
}



这是我从中获取数据的表格:

 CREATE TABLE [dbo]。[AvailableCourses](
[CourseID] [char](10)NOT NULL,
[ClassID] [nvarchar](50)NULL ,
[courseName] [nvarchar](50)NOT NULL,
[education] [nvarchar](50)NOT NULL,
PRIMARY KEY CLUSTERED

[ CourseID] ASC





我尝试过:



有没有人知道为什么不工作?我也尝试过SelectedIndex和SelectedValue但是什么也没做,它什么都不做。谢谢你。

解决方案

您不使用该代码中的组合框选择来决定要访问的表的哪一行 - 您作为DisplayMember和ValueMember访问的列不在数据库表中!

可能你需要

 comboBoxEducation.DisplayMember =courseName; 
comboBoxEducation.ValueMember =Co urseID;



然后使用 comboBoxEducation 的SelectedIndexChanged事件来更新您的复选框。


我设法找到问题。我有一个方法可以使用所有特定于组合框的值的课程,首先我从SelectedIndexChanged中删除了LoadEducation()组合框并添加了将该课程特定于该教育的方法。

 private void comboBox1_SelectedIndexChanged(object sender,EventArgs e)
{
this.PopulateCheckBoxes ();

}



之后,我将其删除(

 PopulateCheckBoxes 

) Form_Load因为它会在已经存在的那些之上选择特定的课程并且之后没有更新(这也是我的一个问题)。我只在Form_Load()中使用了LoadEducation()方法并且我从

 private void comboBox1_SelectedIndexChanged(object sender,EventArgs e)

。现在它对我来说非常合适。感谢大家的帮助,我希望这对有同样问题的年轻开发者有用


I have a project in winforms and in my RegisterStudent form I have a combobox that hold education column.Based on the selection from the combobox,a selection of checkboxes will appear.The problem is that when i want to select a value from the combobox,i can't.Instead the courses for the default value appears.This is my method to load data into the combobox:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    LoadEducation();
}
private void LoadEducation()
{
    using (SqlConnection conn = new SqlConnection(connstr))
    {
        try
        {
            string query = "select  education from AvailableCourses";

            da = new SqlDataAdapter(query, conn);
            conn.Open();
            ds = new DataSet();
            da.Fill(ds, "AvailableCourses");
            comboBoxEducation.DisplayMember = "education";
            comboBoxEducation.ValueMember = "education";
            comboBoxEducation.DataSource = ds.Tables["AvailableCourses"];
        }
        catch (Exception ex)
        {
            // write exception info to log or anything else
            MessageBox.Show("Error occured!");
        }
    }
}


And this is the table from where i take the data:

CREATE TABLE [dbo].[AvailableCourses](
	[CourseID] [char](10) NOT NULL,
	[ClassID] [nvarchar](50) NULL,
	[courseName] [nvarchar](50) NOT NULL,
	[education] [nvarchar](50) NOT NULL,
PRIMARY KEY CLUSTERED 
(
	[CourseID] ASC



What I have tried:

Does anybody have any idea why is not working?I have also tried with SelectedIndex and SelectedValue but for nothing,it won't do anything.Thank you in advance.

解决方案

You don't use the combobox selection in that code to decide which row of your table to access - and the columns you access as a DisplayMember and ValueMember are not in your database table!
Probably, you need

comboBoxEducation.DisplayMember = "courseName";
comboBoxEducation.ValueMember = "CourseID";


And then to use the SelectedIndexChanged event of comboBoxEducation to update your checkboxes.


I have manage to find the problem.I had a method that would take all the courses specific to the value of the combobox and,first of all I deleted the LoadEducation() from the SelectedIndexChanged for combobox and added the method which takes the courses spcific to that education.

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
      {
          this.PopulateCheckBoxes();

      }


After,I deleted it(

PopulateCheckBoxes

) from Form_Load because it would have selected the specific courses on top of the ones already there and would have not updated after(that was also one of my problems).I only had the method LoadEducation() in Form_Load() and I deleted it from

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)

.Now it works perfectly for me.Thank you everyone for your help and I hope this could be useful for young developers with the same problem.


这篇关于为什么我无法从组合框中选择值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆