从DB加载DataGrid时出现InvalidOperationException

时间:2014-04-19 12:59:49

标签: c# wpf oracle mvvm

我的WPF / MVVM应用程序出现问题。当我想将数据网格与数据库连接时,每次都会出现Xaml Parse异常。

我有不同的标签,我希望目前在一个标签项(XAML中的UserControl)上显示我的数据网格。

My Model Properties:
class ModelEmp : ViewModelBase
    {
        private string _EMPLOYEE_NAME;
        private string _LAST_NAME;
        private string _EMAIL;
        private string _Department_ID;
        private string _Location_ID;
        private string _Department_Name;
        private string _Manager_ID;


        public string EMPLOYEE_NAME     { get { return _EMPLOYEE_NAME; } set { _EMPLOYEE_NAME = value; OnPropertyChanged("EMPLOYEE_NAME"); } }
        public string LAST_NAME         { get { return _LAST_NAME; } set { _LAST_NAME = value; OnPropertyChanged("LAST_NAME"); } }
        public string EMAIL             { get { return _EMAIL; } set { _EMAIL = value; OnPropertyChanged("EMAIL"); } }
        public string Department_ID     { get { return _Department_ID; } set { _Department_ID = value; OnPropertyChanged("Department_ID"); } }
        public string Location_ID       { get { return _Location_ID; } set { _Location_ID = value; OnPropertyChanged("Location_ID"); } }
        public string Department_Name   { get { return _Department_Name; } set { _Department_Name = value; OnPropertyChanged("Department_Name"); } }
        public string Manager_ID        { get { return _Manager_ID; } set { _Manager_ID = value; OnPropertyChanged("Manager_ID"); } }

    }

这是我的ViewModelDataGrid类和我的方法:

private ObservableCollection<DataTable> _datagrid;

public ObservableCollection<DataTable> datagrid { get { return this._datagrid; } set { this._datagrid = value; OnPropertyChanged("datagrid"); } }

public DataGridViewModel()
{
    this._datagrid = new ObservableCollection<DataTable>();
    datagridmethod();
}

//I dont know if it is also ok...
public void datagridmethod()
{
     DB conlog = new DB(Global.tsname_p, Global.datenbank_p, Global.password_p); // Parameters for my database connection DB is an other class so i can connect with my db..., on my Class Global is the Name of the db etc.
     DataTable dd;
     OracleDataAdapter dp = new OracleDataAdapter();
     OracleCommand Comm = new OracleCommand();
     conlog.connect();
     dd = conlog.SelectQuery("SELECT DEPARTMENTS.DEPARTMENT_ID, DEPARTMENTS.LOCATION_ID, DEPARTMENTS.DEPARTMENT_NAME,
DEPARTMENTS.MANAGER_ID, EMPLOYEES.FIRST_NAME, EMPLOYEES.LAST_NAME,
EMPLOYEES.EMAIL  FROM DEPARTMENTS, EMPLOYEES  WHERE
DEPARTMENTS.DEPARTMENT_ID ='90'");
     dp.SelectCommand = Comm;

     // InvalidOperation exception occurs here. I dont know if this is the right way to do this but I cannot find an other solution, and ofc I dont want the code behind thing with the name of the datagrid.
      dp.Fill(dd); 
  }

这是我的XAML:

<DataGrid DataContext="{Binding datagrid}" RowBackground="LightYellow" AlternatingRowBackground="LightBlue"  AutoGenerateColumns="False" HorizontalAlignment="Center" ItemsSource="{Binding}"  >
    <DataGrid.Columns>
        <DataGridTextColumn Header="Department ID"           Binding="{Binding Path=Department_ID}"    />
        <DataGridTextColumn Header="Department Name"         Binding="{Binding Path=Department_Name}"  />
        <DataGridTextColumn Header="Manager ID"              Binding="{Binding Path=Manager_ID}"       />
        <DataGridTextColumn Header="Location ID"             Binding="{Binding Path=Location_ID}"      />
        <DataGridTextColumn Header="First Name"              Binding="{Binding Path=EMPLOYEE_NAME}"    />
        <DataGridTextColumn Header="Last Name"               Binding="{Binding Path=LAST_NAME}"        />
        <DataGridTextColumn Header="Email Adress"            Binding="{Binding Path=EMAIL}"             />
    </DataGrid.Columns>
</DataGrid>

以下是我的错误的屏幕截图:http://oi57.tinypic.com/2uy64qs.jpg

任何帮助。提前谢谢!

不会出现错误,但在我的数据网格上没有显示任何内容,是否可以使用我的绑定?

public void comboconnectselectedfield()
        {
            string connstr = "******";
            OracleConnection conn = new OracleConnection(connstr);
            OracleCommand cmd = new OracleCommand();
            string sel = "SELECT DEPARTMENTS.DEPARTMENT_ID, DEPARTMENTS.LOCATION_ID, DEPARTMENTS.DEPARTMENT_NAME, DEPARTMENTS.MANAGER_ID, EMPLOYEES.FIRST_NAME, EMPLOYEES.LAST_NAME, EMPLOYEES.EMAIL  FROM DEPARTMENTS, EMPLOYEES  WHERE DEPARTMENTS.DEPARTMENT_ID ='90'";
            OracleDataAdapter empadap = new OracleDataAdapter();
            empadap.SelectCommand = new OracleCommand(sel,conn);
            DataTable dta = new DataTable();
            empadap.Fill(dta);
        }

0 个答案:

没有答案