VC#这个页面实现的是员工信息管理,包括几个TextBox控件和一个DataGridView  ,它们都绑定了同一个tb_employeeBindingSource,还有一个PictureBox用来显示员工照片,数据库里存放的是照片地址,
//这段代码我想实现的是DataGridView中当前项记录发生变化时,会出发 CurrentChanged事件,通过处理该事件,从文件系统中加载不同的相片到PictureBox控件中 我的问题是我该如何调用这段代码呢?在哪调用还是添加事件,怎么添加事件?
        private void tb_employeeBindingSource_CurrentChange(object sender, EventArgs e)
        {
            DataRowView currentRowView = tb_employeeBindingSource.Current as DataRowView;
            db_cantingDataSetEM.tb_employeeRow currentrow = currentRowView.Row as db_cantingDataSetEM.tb_employeeRow;            if (string.IsNullOrEmpty(currentrow[db_cantingDataSetEM.tb_employee.EmpPhotoColumn].ToString()))
            {
                System.ComponentModel.ComponentResourceManager(typeof(db_cantingDataSetEM));
                employeepicture.Image = ((System.Drawing.Image)(Resources.GetObject("employeepicture.Image")));            }
            else
            {
                employeepicture.Image = Image.FromFile(currentrow.EmpPhoto);
            }
        }//上传图片功能
        private void uploadpicture_Click(object sender, EventArgs e)
        {
            OpenFileDialog open= new OpenFileDialog();
            open.InitialDirectory = @"D:\我的文档\图片收藏\";
            open.ShowDialog() ;
            if (open.FileName!=null)
            {
                employeepicture.Image = Image.FromFile(open.FileName);
                string path = System.IO.Path.GetDirectoryName(Application.ExecutablePath) + "\\picture\\" + System.IO.Path.GetFileName(open.FileName);
                System.IO.File.Copy(open.FileName, path, true);
                DataRowView currentRowView = dbcantingDataSetBindingSource.Current as DataRowView;
                db_cantingDataSetEM.tb_employeeRow currentrow = currentRowView.Row as db_cantingDataSetEM.tb_employeeRow;    //上传图片时,这里会报错“未将对象引用设置到对象的实例。”                currentrow.EmpPhoto = path;
            }
        }

解决方案 »

  1.   

    Application.ExecutablePath???這個好象获取的是带EXE名的吧
    Application.StartupPath才是获取程序路径
      

  2.   

    db_cantingDataSetEM.tb_employeeRow currentrow = currentRowView.Row as db_cantingDataSetEM.tb_employeeRow; 这句报错,说明currentRowView==null,才会报这个错误.
    dbcantingDataSetBindingSource.Current的值是怎么来的?
      

  3.   

    你设断点看下dbcantingDataSetBindingSource.Current的值就知道问题了,如果不是你希望的值那就换种方式进行赋值