关于dataGrid查询按钮的实现

纠结了好久的数据查询,今天终于有点进展,虽然还没有能够做出登陆程序,但是我觉得已经非常接近了。

domain server.cs这个类,里面的成员,你会发现,里面有查询,修改,删除,更新这些功能

 public IQueryable<contents> GetContents()
        {
            return this.ObjectContext.contents;
        }

 这是查询的功能,返回的是一个contents实体,如果你要修改,返回你想指定的实体,那么你要这样修改

 public IQueryable<contents> GetContentsByType(string type) {
            return this.ObjectContext.contents.Where(c=>c.name==type);
        }

 上面返回的是经过查询的实体,调用的时候,你只需要调用这么方法

 

插入:

public void InsertContents(contents contents)
        {
            if ((contents.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(contents, EntityState.Added);
            }
            else
            {
                this.ObjectContext.contents.AddObject(contents);
            }
        }

 更新:

public void UpdateContents(contents currentcontents)
        {
            this.ObjectContext.contents.AttachAsModified(currentcontents, this.ChangeSet.GetOriginal(currentcontents));
        }

 删除:

public void DeleteContents(contents contents)
        {
            if ((contents.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(contents, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.contents.Attach(contents);
                this.ObjectContext.contents.DeleteObject(contents);
            }
        }

 用datagrid作例子:代码页很简单

namespace testBox
{
    public partial class MainPage : UserControl
    {
        testBoxDomainService1 test;
        public MainPage()
        {
            test = new testBoxDomainService1();
            InitializeComponent();
            binddata();
          
        }
        private void binddata() {
            dataGrid2.ItemsSource = test.contents;
            test.Load(test.GetContentsQuery());
            
        
        }

        private void chaxun_click(object sender, RoutedEventArgs e)
        {
            testBoxDomainService1 test2 = new testBoxDomainService1();
            string s =textBox1.Text;
            dataGrid1.ItemsSource = test2.contents;
            test2.Load(test2.GetContentsByTypeQuery(s));
        }
    }
}

 前台只需要把autogeneratecolumn设置为true,让其自动生成

就可以实现这样的功能:

转载于:https://www.cnblogs.com/lihaozhou/archive/2012/07/25/2608365.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值