惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Schneier on Security
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
T
Threat Research - Cisco Blogs
C
Cyber Attacks, Cyber Crime and Cyber Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
A
Arctic Wolf
Security Latest
Security Latest
Simon Willison's Weblog
Simon Willison's Weblog
I
Intezer
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Troy Hunt's Blog
Latest news
Latest news
Help Net Security
Help Net Security
S
Security Affairs
Webroot Blog
Webroot Blog
The Hacker News
The Hacker News
AI
AI
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Tor Project blog
Forbes - Security
Forbes - Security
Google DeepMind News
Google DeepMind News
AWS News Blog
AWS News Blog
Attack and Defense Labs
Attack and Defense Labs
P
Proofpoint News Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
H
Help Net Security
L
Lohrmann on Cybersecurity
S
SegmentFault 最新的问题
Google Online Security Blog
Google Online Security Blog
MongoDB | Blog
MongoDB | Blog
Cyberwarzone
Cyberwarzone
The Last Watchdog
The Last Watchdog
S
Securelist
N
News and Events Feed by Topic
S
Secure Thoughts
F
Fortinet All Blogs
博客园_首页
C
Cybersecurity and Infrastructure Security Agency CISA
量子位
M
MIT News - Artificial intelligence
F
Full Disclosure
T
The Blog of Author Tim Ferriss
T
Tailwind CSS Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Microsoft Security Blog
Microsoft Security Blog
I
InfoQ
P
Privacy International News Feed
L
LangChain Blog
Know Your Adversary
Know Your Adversary
C
CERT Recently Published Vulnerability Notes

博客园 - 阿修罗一平

IIS部署WCF出现的各种问题汇总 Infragistics控件的工具栏注册 NUnit的使用中可能遇到的问题 Linq备忘 Nihibernate的重要知识点 Devexpress使用记录 使用List数据集合,利用DevExpress.XtraReports开发Master-Detail报表 PDA访问WCF PDA(WinCE)项目开发中遇到的问题及解决方法总结 Grid控件绑定bindingSource后在新增行时设置Cell的初始值 sqlservier2005转成sqlserver2000中出现的问题(WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]) 项目考虑因素及解决方案(.net) 设计模式在工作中的应用(三) 设计模式在工作中的应用(二) 设计模式在工作中的应用(一) NHibernate示例 Remoting服务集成到IIS的简单总结 关于Remoting服务启动和停止的简单总结 将公司系统从SqlServer 2K移植到Oracle 10g中的简要总结
“BindingSource绑定单个实体对象后在代码中赋值无效和无法显示”的解决方法
阿修罗一平 · 2009-09-28 · via 博客园 - 阿修罗一平

    问题不是高深的问题,只是希望能静下心来记录项目中所遇到的种种问题。

    今天同事在开发过程中,使用bindingsource绑定一个对象,出现了一点问题。一般情况下,我们都会在界面控件上输入和修改数据,那么bindingsource所绑定的对象也会随着界面控件值的改变而改变。但是我的同事却出现了一个问题,如果是在代码中修改界面控件的值,再获取bindingsource所指向对象的值却没有改变;或者直接修改bindingsource所指向对象的值界面上的控件的值却没有随着改变。
    仔细想想,应该是不论在对谁进行复制的时候,需要一个方法或者一个事件更新对象和界面,使对象和界面的值都保持一致。吃完饭后,做了一个例子,试了一些方法。其他的方法也能解决这种问题,并不是唯一的方法。

 定义了一个简单的类:

public class Entity1
    {
        private string m_Name = string.Empty;
        public string Name
        {
            get
            {
                return m_Name;
            }
            set
            {
                m_Name = value;
            }
        }

        public int m_Age = 0;
        public int Age
        {
            get
            {
                return m_Age;
            }
            set
            {
                m_Age = value;
            }
        }
    }

窗体类定了一个成员变量 private Entity1 bind1 = new Entity1();
窗体初始化的代码:this.bindingSource1.DataSource = bind1;

   第一种情况:在事件处理代码中对控件赋值,比如:

            textEdit1.Text = "Control1";
            textEdit2.Text = "11";

            //对控件赋值的处理
            this.bindingSource1.EndEdit();
   通过调用“this.bindingSource1.EndEdit();”,bindingSource1所指向的对象bind1的值会进行更新,在后续的程序访问bind1就会是最新的值。如果不调用“this.bindingSource1.EndEdit();”,那么bind1的值将不会随着控件值的修改而进行修改。

  第二中情况:在事件处理代码中对所绑定对象进行赋值,比如

            bind1.Name = "program1";
            bind1.Age = 22;

            //对程序赋值的处理
            this.bindingSource1.ResetBindings(false);

  通过调用“this.bindingSource1.ResetBindings(false);”,界面控件上的值会进行更新,保持binding和界面控件的一致。如果不调用“this.bindingSource1.ResetBindings(false);”,界面上控件的值不会随着bind1的值的修改而更新。