本篇以DataForm控件,来说明下Wcf Ria Service中的增删改查操作。利用DataForm控件还是比较方便的!以Student实体为例,直接贴代码了,代码里附有一些注释!

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
//添加命名空间
using System.ServiceModel.DomainServices.Client;
using ScoreManagement.Web;namespace ScoreManagement
{enum ButtonModel{ Edit,Add,Cancel}public partial class Test2 : UserControl{ButtonModel buttonModel = ButtonModel.Cancel;     DomainService1 client = new DomainService1();public Test2(){InitializeComponent();this.Loaded += new RoutedEventHandler(StudentInfoManage_Loaded);}void StudentInfoManage_Loaded(object sender, RoutedEventArgs e){//把一个entityset绑定到dataform,添加删除按钮均不可用是什么原因//因为client.Students 是 EntitySet类型的,它并没有实现INotifyCollectionchanged以及iNotifyPropertyChanged接口。//this.dataForm1.ItemsSource = client.Students;//client.Load(client.GetStudentsQuery());//所以用如下方法:          LoadOperation<Student> lo = client.Load(client.GetStudentsQuery());lo.Completed += new EventHandler(lo_Completed);}void lo_Completed(object sender, EventArgs e){dataForm1.CommitButtonContent = "保存";dataForm1.CancelButtonContent = "取消";dataForm1.AutoEdit = false;dataForm1.AutoCommit = true;LoadOperation<Student> lo = (LoadOperation<Student>)sender;//这里如果是 this.dataForm1.ItemsSource = lo.Entities,还是不能进行 “添加"和“删除”操作的.一定要ToList()下dataForm1.ItemsSource = lo.Entities.ToList();        //dataForm1.CurrentItem = lo.Entities.FirstOrDefault();dataForm1.DeletingItem += new EventHandler<System.ComponentModel.CancelEventArgs>(dataForm1_DeletingItem);         dataForm1.EditEnded += new EventHandler<DataFormEditEndedEventArgs>(dataForm1_EditEnded);dataForm1.CommandButtonsVisibility = DataFormCommandButtonsVisibility.All;dataForm1.AddingNewItem+=new EventHandler<DataFormAddingNewItemEventArgs>(dataForm1_AddingNewItem);dataForm1.BeginningEdit += new EventHandler<System.ComponentModel.CancelEventArgs>(dataForm1_BeginningEdit);   }//设置编辑模式void dataForm1_BeginningEdit(object sender, System.ComponentModel.CancelEventArgs e){buttonModel = ButtonModel.Edit;}   //点"+"后的编辑模板,如果是“Commit”操作void dataForm1_EditEnded(object sender, DataFormEditEndedEventArgs e){//如果提交 把该项添加到数据库if (e.EditAction == DataFormEditAction.Commit){//添加if (buttonModel == ButtonModel.Add){Student student = dataForm1.CurrentItem as Student;client.Students.Add(student);client.SubmitChanges();}//编辑if (buttonModel == ButtonModel.Edit){Student student = dataForm1.CurrentItem as Student;                  client.SubmitChanges();}}//取消if (e.EditAction == DataFormEditAction.Cancel){buttonModel = ButtonModel.Cancel;}}//设置添加模式void dataForm1_AddingNewItem(object sender, DataFormAddingNewItemEventArgs e){//设置为添加模式buttonModel = ButtonModel.Add;}//删除void dataForm1_DeletingItem(object sender, System.ComponentModel.CancelEventArgs e){//MessageBoxResult result= MessageBox.Show("你确定要删除该项吗?", "提示", MessageBoxButton.OKCancel);//if (result == MessageBoxResult.OK)//{//    //MessageBox.Show("恭喜你!删除成功!");//}
            Student student = dataForm1.CurrentItem as Student;client.Students.Remove(student);client.SubmitChanges(OnSubmitCompleted, null);           }//异常处理public void OnSubmitCompleted(SubmitOperation so){if (so.Error != null){string message = so.Error.Message;if (so.EntitiesInError.Any()){message = string.Empty;Entity entityError = so.EntitiesInError.First();if (entityError.EntityConflict != null){EntityConflict confict = entityError.EntityConflict;foreach (string s in confict.PropertyNames){message += string.Format("Member '{0}' in confict /r/n", s);}}else if(entityError.ValidationErrors.Any()){foreach (System.ComponentModel.DataAnnotations.ValidationResult vr in entityError.ValidationErrors){message += string.Format("{0}/r/n",vr.ErrorMessage);}}}MessageBox.Show(message,"操作失败!",MessageBoxButton.OK);}}}
}

 

RIA Service + dataformc操作例子相关推荐

  1. RIA Service 的 SOAP EndPoint

    原文 www.cyqdata.com/cnblogs/article-detail-39983-english 越来越多的朋友都在使用Silverlight开发应用程序,其中我们常用的还会有一个特殊的 ...

  2. 使用Fiddler解析WCF RIA Service传输的数据

    使用Fiddler 2 解析WCF RIA Service传输的数据,您需要安装一个PlugIn.下载解压后,放在Inspectors文件中.重新启动Fiddler,你就会看到WCF Binary的选 ...

  3. Silverlight中服务通信方式的选择(WCF、Data Service、Ria Service)

    Silverlight中服务通信方式的选择(WCF.Data Service.Ria Service) 转自 http://www.cnblogs.com/024hi/archive/2011/06/ ...

  4. pb对Web Service的操作可使用两种方式实现

    从PB8.0/9.0开始,就已经提供Web Service Proxy功能,能够直接进行相关程序的编写. 但是,部分老项目使用PB6.5开发 研究后发现,其实PB6.5要操作Web Service也挺 ...

  5. firewalld的9个zone、firewalld关于zone和service的操作

    firewalld的9个zone centos7 默认的防火墙工具为firewalld 打开firewalld [root@localhost ~]# systemctl disable iptabl ...

  6. iptables规则备份和恢复、firewall的zone的操作、service的操作

    iptables规则备份和恢复 保存和备份iptables规则如下: service iptables save //会把规则保存到/etc/sysconfig/iptables 把iptables规 ...

  7. ASP.NET 网站项目 EF 的简单操作例子

    ASP.NET 网站项目 EF 的简单操作例子: 操作代码: using EFTest.Models; using System; using System.Collections.Generic; ...

  8. 2018年第23周-大数据的HDFS数据流及操作例子

    上周已经把Hadoop的HDFS的架构和设计大概说了下,也有部署过程.在这周讲的HDFS的数据流及操作例子 HDFS数据流 HDFS系统依赖于以下服务 1.NameNode 2.DataNode 3. ...

  9. oracle首先创建三个表,oracle理论学习详解及各种简单操作例子(初学者必备)

    oracle理论学习详解及各种简单操作例子(菜鸟必备) 1.数据库的发展过程 层次模型 -->网状模型 -->关系模型 -->对象关系模型 2.关于数据库的概念 DB:数据库(存储信 ...

最新文章

  1. python函数调用的例子_实例讲解Python中函数的调用与定义
  2. PHP获取二维数组中某一列的值集合
  3. NSUserDefaults
  4. ObjectArx开发对txt文本文件的操作一例
  5. linux操作系统怎么修改时区,Linux操作系统修改时区的方法
  6. 计算机系班级海报,系部动态 | 电子系“五彩班栏”班级海报评比
  7. cstring越界_CString 内核
  8. 阿里云天池 零基础入门NLP - 新闻文本分类 2种做法,F1=0.87
  9. 【leetcode】数组建树
  10. 笔记:MapReduce Design Patterns
  11. 域名邮箱和邮件服务器
  12. 怎样用c语言实现CPU超频,cpu超频教程【操作流程】
  13. 回收站清空了怎么恢复?快来get实用方法!
  14. [转]项目实施过程中的风险控制
  15. 【风马一族_mysql】mysql基本指令
  16. Your system is missing DirectX components
  17. 基于Jsp+Servlet的户籍管理系统(JavaWeb毕业设计、课程设计)
  18. vb.net自动发帖器2(httpwebrequest实现)
  19. python发送免费短信验证码(腾讯云 短信)
  20. android核心技术之性能分析工具TraceView

热门文章

  1. charles 安装 ssl_前端开发如何使用抓包工具 charles
  2. 计算机网络环境及应用系统的安装与调试(Computer network environment and application system installation and debugging)
  3. TCL中关于Cells的一些使用方法?
  4. IC基础知识(2)模拟和数字电子学导论
  5. DSP专家给你一个选择FPGA的理由(让人看了热血沸腾的一篇科普文章)
  6. Struts2 @ResultPath注释示例
  7. Linux下基于官方源代码RPM包构建自定义MySQL RPM包
  8. Python_017 GUI编程
  9. iOS开发 开发者证书申请及上架流程
  10. iptables实现路由转发