none
WPF中的DataGrid控件中增加数据 RRS feed

  • 问题

  • private void DataGrid1_DataGrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
            {
                Employee employee = e.Row.DataContext as Employee;
                if (e.EditAction == DataGridEditAction.Commit)
                {
                    using (CQDBEXT DB = new CQDBEXT())
                    {
                        Employee matchedData = DB.Employee.SingleOrDefault(zl => zl.id == employee.id);
                        if (matchedData == null)
                        {
                            matchedData = new Employee();

                            DB.InsertOnSubmit(matchedData);

                        }

                        matcheData.name = employee.name;

                             DB.SubmitChanges();
                        }

                   }

    运行后在datagrid中的空行输入数据,总是提示不能访问已释放的对象,请问如何解决?

     

    2011年11月6日 15:00

答案

  • 我看了你的代码,我没法重现你的问题,不过你可以尝试把你的代码放大dispatch里看看能解决你的问题么:

    this.Dispatcher.BeginInvoke((Action)(() =>
      {
         // your code here;
      }), null);

    如果不行,你要分析一下错误信息了,这个我们这边是很难重现的。


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年11月9日 7:55
    版主