zoukankan      html  css  js  c++  java
  • 关于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,让其自动生成

    就可以实现这样的功能:

  • 相关阅读:
    让DateTimePicker显示空时间值 (转) 武胜
    Webcam in C#: AForge.NET (转) 武胜
    C#自定义事件的步骤 武胜
    交换机VLAN的配置 (转) 武胜
    虚拟LAN安全的最佳实践经验 (转) 武胜
    绿色版 MySQL 的安装配置 (转) 武胜
    net 中捕获摄像头视频的方式及对比(How to Capture Camera Video via .Net) (转) 武胜
    C# WndProc的使用方法 (转) 武胜
    金融证券业Windows NT服务器热备份系统工作示意图 (转) 武胜
    ubuntu下配置samba实现文件夹共享
  • 原文地址:https://www.cnblogs.com/lihaozhou/p/2608365.html
Copyright © 2011-2022 走看看