zoukankan      html  css  js  c++  java
  • .net数据绑定问题

    最近在使用Ado.net Entity Framework和Linq to entities时遇到大量的winform实体数据绑定问题,部分暂时还未能解决,现暂时记录于此,逐步解决。

    1.匿名类型不能绑定

    现在怀疑匿名类型的属性全部是以字段形式保存在内存中的,而winform空间是不支持字段绑定的,所有的Datamember必须是属性类型。因此,以下代码中被注释的部分在绑定时无法对应到控件的DataPropertyName,而下面的部分则可以。

    Here is a technique for binding an arraylist of objects where the objects contain public property that can appear as columns in the datagrid.

    有人知道更详细的么?

    代码
            //public string AreaCode = string.Empty;
            
    //public string AreaName = string.Empty;
            
    //public string AreaDesc = string.Empty;
            
    //public string TestCode = string.Empty;
            
    //public string TestName = string.Empty;
            
    //public string TestDesc = string.Empty;

            
    public string AreaCode { getset; }
            
    public string AreaName { getset; }
            
    public string AreaDesc { getset; }
            
    public string TestCode { getset; }
            
    public string TestName { getset; }
            
    public string TestDesc { getset; }

    2. 截断两个数据绑定之间的关联

    If you have two controls bound to the same datasource, and you do not want them to share the same position, then you must make sure that the BindingContext member of one control differs from the BindingContext member of the other control. If they have the same BindingContext, they will share the same position in the datasource.

    代码
    private void Form1_Load(object sender, System.EventArgs e)

         
    //get a datatable somehow... 
         this.myDataTable = GetATable(); 

         
    this.dataGrid1.DataSource = myDataTable; 
     
         
    //set a new binding context for the combobox  
         this.comboBox1.BindingContext = new BindingContext();  
         
    this.comboBox1.DataSource = myDataTable;  
         
    this.comboBox1.DisplayMember = "Col1";  
         
    this.comboBox1.ValueMember = "Col1";  

    3. CSV文件可以直接以Excel表格的方式直接读取。
    4. 绑定到实体是如果不支持双向绑定(two-way binding),应该使用CurrencyManager来刷新。
       
    代码
              //use currency manger to sync up the listbox  
              BindingManagerBase bm 
    = this.listBox1.BindingContext[myArrayList];  
              CurrencyManager cm 
    = (CurrencyManager) bm;  
              
    if (cm != null)  
                   cm.Refresh();                 
     
              
    //Or, you can just reset the datasource 
              
    //listBox1.DataSource = null;  
              
    //listBox1.DataSource = myArrayList;  

    可以参照这篇文档,提到了两个数据绑定的问题。

      绿森林  http://www.cnblogs.com/greeny/archive/2010/01/26/1656431.html

    1. LINQ的查询结果无法直接作为DataGridView的数据源

    2. 字符串无法直接作为DataGridView的数据源


     

  • 相关阅读:
    RecSys Challenge 2015
    Python 多行注释
    编译型语言与解释型语言
    vs2012 提示 未能正确加载 "Visual C++ Language Manager Package" 包
    人工智能 VS 机器学习 VS 深度学习
    CV-视频分析:静态背景下的运动检测
    消费者做出购买决策的流程
    Fat jar用途
    Eclipse中打包插件Fat Jar的安装与使用
    Eclipse将引用了第三方jar包的Java项目打包成jar文件的两种方法
  • 原文地址:https://www.cnblogs.com/tukzer/p/1740631.html
Copyright © 2011-2022 走看看