关于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,让其自动生成

就可以实现这样的功能:

posted @ 2012-07-25 15:20  菠萝君  阅读(1067)  评论(0编辑  收藏  举报