2014-10-07 65 views
0

我在SQL Server数据库中有一个名为“course”的表,其中包含60个课程。如何在TextBox中显示ComboBox中的值?

该表包含两列:COURSE_ID (PK), COURSE_NAME

我得到使用存储过程从数据库中的所有课程:

create PROC [dbo].[GET_ALL_COURSES] 
AS 
SELECT * FROM COURSE 

和填充ComboBox这样的:

public FORM_CRS() 
{ 
    InitializeComponent(); 

    CMB_COURSE.DataSource = CRS.GET_ALL_COURSES(); 
    CMB_COURSE.DisplayMember = "COURSE_NAME"; 
    CMB_COURSE.ValueMember = "COURSE_ID"; 
} 

当我点击ComboBox中的课程我想在中显示该课程的,但我不知道如何。

回答

1

创建您的组合框,如onSelectedIndexChanged事件..

void cmbCourse_onSelectedIndexChanged(object sender,EventArgs e) { 
yourTextBoxName.Text=CMB_COURSE.SelectedItem.ToString();} 
+0

太感谢你了:) ..它由真实事件SelectedValueChanged解决 – 2014-10-07 23:12:45

0
CMB.Click(object sender, EventArgs e) //click, changedvalue or something else what you want - change it for your neccesity 
{ 
    txt.Text = ((ComboBoxItem)CMB_COURSE.SelectedItem).Content.ToString(); 
    //or, i can't test it now, well do it and edit this answer 
    txt.Text = CMB_COURSE.SelectedValue.ToString(); 
    //or: 
    DataRowView row = (DataRowView)CMB_COURSE.SelectedItem; 
    if (row != null) 
    { 
     txt.Text = row[0] + " " + row[1]; 
    } 
} 

最后的解决方案的从这里: https://stackoverflow.com/a/8518930/3810460

+0

谢谢老兄..它由真实事件SelectedValueChanged解决 – 2014-10-07 23:11:23

1

订阅SelectedValueChanged事件填充你的ComboBox后:

CMB_COURSE.DataSource = CRS.GET_ALL_COURSES(); 
CMB_COURSE.DisplayMember = "COURSE_NAME"; 
CMB_COURSE.ValueMember = "COURSE_ID"; 

CMB_COURSE.SelectedValueChanged += (s, e) 
    => textBox1.Text = CMB_COURSE.SelectedValue.ToString(); 

当您的ComboBox中的选择更改时,您的TextBox将显示当前值COURSE_ID的值。

+0

非常感谢你:) – 2014-10-07 23:10:09

0

首先,我认为你应该检查这个link作为参考。

接下来,认为这个代码段相匹配,你在找什么:

private List<Course> Courses = new List<Course>(); 

    public Form1() 
    { 
     InitializeComponent(); 

     Courses.Add(new Course("Computer Science",1)); 
     Courses.Add(new Course("English", 2)); 
     Courses.Add(new Course("Art", 3)); 
     Courses.Add(new Course("Biology", 4)); 
     Courses.Add(new Course("Philosophy", 5)); 
     Courses.Add(new Course("Humanities", 6)); 

     CourseCB.DataSource = Courses; 
     CourseCB.SelectedText = Courses.ElementAt(0).CourseName; 
     CourseCB.DisplayMember = "CourseName"; 
     CourseCB.ValueMember = "CourseID"; 
    } 

    private void OnValueChanged(object sender, EventArgs e) 
    { 
     if (CourseCB.SelectedIndex != -1) 
     { 
      CourseIDTB.Text = CourseCB.SelectedValue.ToString(); 
     } 
    } 

    public class Course 
    { 
     private string myCourseName; 
     private int myCourseID; 

     public string CourseName 
     { 
      get { return this.myCourseName; } 
      set { this.myCourseName = value; } 
     } 

     public int CourseID 
     { 
      get { return this.myCourseID; } 
      set { this.myCourseID = value; } 
     } 

     public Course(string name, int ID) 
     { 
      this.CourseName = name; 
      this.CourseID = ID; 
     } 

    } 

你有正确的格式,但你需要从SQL提供一个对象的属性值不正确地使用它引用DisplayMember和ValueMember值。

我建议您执行查询,然后将值插入Course对象中,然后您就可以使用此代码。

+0

谢谢老兄..它由真实事件SelectedValueChanged解决。 – 2014-10-07 23:17:23

相关文章