我这里有 一个x:Name="sdf"  ComboBox 下拉框控件. ComboBox  显示的值为具体的 coursename .当下拉框SelectionChanged 时. private void sdf_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var course = this.sdf.SelectedItem;//这里也获取到 string 类型的值
            CourseID = ((Course)course).courseid; //这里就抱错了.提出 无法将类型为“System.String”的对象强制转换为类型“StudentScoreManage.StudentWebServer.Course”        }
Course 类 public class Course
    {
        public int courseid { get; set; }
        public string coursename { get; set; }
}
-----------
我将course转换为Course 类  应该是可以的.请各位高手给点提示或思想!谢谢!

解决方案 »

  1.   

    不可以  可以不就不会出错了吗
    你可以先实例化一个Course,然后把coursename的值赋进去LZ结贴吧~ 
      

  2.   


    http://www.cnblogs.com/kaixun001/archive/2009/02/17/1392817.html这篇文章  作者可以完成阿!
      

  3.   


    Course 实例 是从数据库中获取到的.也就是 ComboBox  coursename 是从数据库来的.
      

  4.   

    不会吧。。这样子转换不出问题才怪了。SelectedItem 这个的值是 id的 还是name的???   如果想要转换int  干嘛不用parse??
      

  5.   

    var course = this.sdf.SelectedItem;//这里也获取到 string 类型的值我Combobox 里面的 下拉框 值为 string  类型. 效果也能出来.
    但我想在另外一个事件里 将获取到的string 类型转换为 对应的 id 值.将起 插入到数据库里.可能是我没有说明白吧!抱歉!
      

  6.   

    在这篇文章里.作者好像也是这样来转换的吧!可能是我没有吸收完全.还请您们指点指点.http://www.cnblogs.com/kaixun001/archive/2009/02/17/1392817.html
     var CitAlly = this.CBox_City.SelectedItem;
                    int CityID = ((city)CitAlly).id;//....
      public struct city
            {
                public int id { get; set; }
                public string name { get; set; }
            }
      

  7.   

    阿!它用的是 struct .
    而我用的是 calss.我再修改修改!
      

  8.   


    struct 与class 的区别不会影响结果吧!??
        private void TabControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                StudentWebServiceSoapClient sws = new StudentWebServiceSoapClient();
                sws.GetSelectCourseNameCompleted += new EventHandler<GetSelectCourseNameCompletedEventArgs>(sws_GetSelectCourseNameCompleted);
                sws.GetSelectCourseNameAsync();
            }        void sws_GetSelectCourseNameCompleted(object sender, GetSelectCourseNameCompletedEventArgs e)
            {
                //throw new NotImplementedException();
                sdf.ItemsSource = e.Result.Select(p => p.coursename);
                sdf.SelectedIndex = 0;
            }
      

  9.   

    var course = this.sdf.SelectedItem;
    这里你先调试看看course的类型是什么
      

  10.   


      抱错 提示
    无法将类型为“System.String”的对象强制转换为类型“StudentScoreManage.StudentWebServer.Course”
    --
    course Type :object {string}
      

  11.   

    那就是说你绑定到ComboBox的只是coursename,而不是Course对象
    sdf.ItemsSource = e.Result;
    试试
    public class Course
    {
            public int courseid { get; set; }
            public string coursename { get; set; }
            public override string ToString()
            {
                return coursename;
            }
    }
      

  12.   


    恩.转换的时候得 转换 Course对象.而不能具体的值.这是可行的.但问题是ComboBox ,  sdf.ItemsSource = e.Result; 返回的值却不为具体的string 值了阿?
      

  13.   

    谢谢ojlovecd 老师点拨.问题解决了.
      

  14.   

    不知道你的SelectionChanged 是哪个控件的事件。我这里拿DataGridView 类的SelectionChanged 来举例说明:
    DataGridView. SelectionChanged 指的是DataGridViewCell 的变更。你可以查MSDN 找到。
    所以this.sdf.SelectedItem是一个DataGridViewCell 对象。你强制转换对象不对应,当然抛出异常了~~~