Databinding in Windows Forms demo (CSWinFormDataBinding)

/************************************* Module Header **************************************\
* Module Name: MainForm.cs
* Project: CSWinFormDataBinding
* Copyright (c) Microsoft Corporation.
*
* The CSWinFormDataBinding sample demonstrates the Windows Forms Data Binding technology.
* Data binding in Windows Forms gives you the means to display and make changes to
* information from a data source in controls on the form. You can bind to both traditional
* data sources as well as almost any structure that contains data.
* Windows Forms can take advantage of two types of data binding:
* simple binding and complex binding. You can refer to this document for more information:
* http://msdn.microsoft.com/en-us/library/c8aebh9k.aspx
*
* This source is subject to the Microsoft Public License.
* See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
* All other rights reserved.
*
* History:
* * 3/13/2009 3:00 PM ZhiXin Ye Created
\******************************************************************************************/ #region Using directives
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
#endregion namespace CSWinFormDataBinding
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
////////////////////////////////////////////////////////////////////////////////
// Simple Binding Examples
// #region Simple Binding Example 1 (Bind to class property) // In this example the CheckBox is the data source
this.textBox1.DataBindings.Add("Text", this.checkBox1, "Checked"); #endregion #region Simple Binding Example 2 (Bind to class property) // In this example the Form itself is the data source.
// With the update mode set to DataSourceUpdateMode.Never the data source won't
// update unless we explicitly call the Binding.WriteValue() method.
Binding bdSize = new Binding("Text", this, "Size", true, DataSourceUpdateMode.Never);
this.textBox2.DataBindings.Add(bdSize);
this.btnSet.Click += delegate(object button, EventArgs args)
{
// Force the value to store in the data source
bdSize.WriteValue();
}; #endregion #region Simple Binding Example 3 (Bind to DataTable field) // DataSource setup:
//
// Create a Table named Test and add 2 columns
// ID: int
// Name: string
//
DataTable dtTest = new DataTable();
dtTest.Columns.Add("ID", typeof(int));
dtTest.Columns.Add("Name", typeof(string)); dtTest.Rows.Add(, "John");
dtTest.Rows.Add(, "Amy");
dtTest.Rows.Add(, "Tony"); BindingSource bsTest = new BindingSource();
bsTest.DataSource = dtTest; // Bind the TextBoxes
this.textBox3.DataBindings.Add("Text", bsTest, "ID");
this.textBox4.DataBindings.Add("Text", bsTest, "Name"); // Handle the button's click event to navigate the binding.
this.btnPrev.Click += delegate(object button, EventArgs args)
{
bsTest.MovePrevious();
};
this.btnNext.Click += delegate(object button, EventArgs args)
{
bsTest.MoveNext();
}; #endregion ////////////////////////////////////////////////////////////////////////////////
// Complex Binding Examples
// #region Complex Binding Example 1 (Diplaying data from database) // This example is done by the Visual Studio designer.
//
// Steps:
// 1. Click the smart tag glyph (Smart Tag Glyph) on the upper-right corner of
// the DataGridView control.
// 2. Click the drop-down arrow for the Choose Data Source option.
// 3. If your project does not already have a data source, click
// "Add Project Data Source.." and follow the steps indicated by the wizard.
// 4. Expand the Other Data Sources and Project Data Sources nodes if they are
// not already expanded, and then select the data source to bind the control to.
// 5. If your data source contains more than one member, such as if you have
// created a DataSet that contains multiple tables, expand the data source,
// and then select the specific member to bind to. #endregion #region Complex Binding Example 2 (Displaying data from business objects) // Data Source Setup: BindingList<Customer> blc = new BindingList<Customer>(); blc.Add(new Customer(, "John", 10.0M));
blc.Add(new Customer(, "Amy", 15.0M));
blc.Add(new Customer(, "Tony", 20.0M)); //Bind the DataGridView to the list of Customers using complex binding.
this.dataGridView2.DataSource = blc; #endregion #region Complex Binding Example 3 (Master/Detail Binding) // Data Source Setup: DataTable dtMaster = new DataTable("Custotmer");
DataTable dtDetail = new DataTable("Order");
dtMaster.Columns.Add("CustomerID", typeof(int));
dtMaster.Columns.Add("CustomerName",typeof(string)); dtDetail.Columns.Add("OrderID", typeof(int));
dtDetail.Columns.Add("OrderDate",typeof(DateTime));
dtDetail.Columns.Add("CustomerID", typeof(int)); for (int j = ; j < ; j++)
{
dtMaster.Rows.Add(j, "Customer " + j.ToString());
dtDetail.Rows.Add(j, DateTime.Now.AddDays(j), j);
dtDetail.Rows.Add(j+, DateTime.Now.AddDays(j+), j);
} // Create a DataSet to hold the two DataTables
DataSet ds = new DataSet();
ds.Tables.Add(dtMaster);
ds.Tables.Add(dtDetail); // Add a relationship to the DataSet
ds.Relations.Add("CustomerOrder",
ds.Tables["Custotmer"].Columns["CustomerID"],
ds.Tables["Order"].Columns["CustomerID"]); BindingSource bsMaster = new BindingSource();
bsMaster.DataSource = ds;
bsMaster.DataMember = "Custotmer"; BindingSource bsDetail = new BindingSource();
// Bind the details data connector to the master data connector,
// using the DataRelation name to filter the information in the
// details table based on the current row in the master table.
bsDetail.DataSource = bsMaster;
bsDetail.DataMember = "CustomerOrder"; this.dgvMaster.DataSource = bsMaster;
this.dgvDetail.DataSource = bsDetail; #endregion
} } #region Customer Class public class Customer
{
// Private variables
private int _id;
private string _name;
private Decimal _rate; // Constructor
public Customer()
{
this.ID = -;
this.Name = string.Empty;
this.Rate = 0.0M;
} public Customer(int id, string name, Decimal rate)
{
this.ID = id;
this.Name = name;
this.Rate = rate;
} // Properties
public int ID
{
get { return _id; }
set { _id = value; }
} public string Name
{
get { return _name; }
set { _name = value; }
} public Decimal Rate
{
get { return _rate; }
set { _rate = value; }
}
} #endregion
}

DataBinding examples的更多相关文章

  1. 数百个 HTML5 例子学习 HT 图形组件 – WebGL 3D 篇

    <数百个 HTML5 例子学习 HT 图形组件 – 拓扑图篇>一文让读者了解了 HT的 2D 拓扑图组件使用,本文将对 HT 的 3D 功能做个综合性的介绍,以便初学者可快速上手使用 HT ...

  2. 数百个 HTML5 例子学习 HT 图形组件 – 拓扑图篇

    HT 是啥:Everything you need to create cutting-edge 2D and 3D visualization. 这口号是当年心目中的产品方向,接着就朝这个方向慢慢打 ...

  3. HTML5 例子学习 HT 图形组件

    HTML5 例子学习 HT 图形组件 HT 是啥:Everything you need to create cutting-edge 2D and 3D visualization. 这口号是当年心 ...

  4. 基于 HTML5 的电力接线图 SCADA 应用

    在电力.油田燃气.供水管网等工业自动化领域 Web SCADA 的概念已经提出了多年,早些年的 Web SCADA 前端技术大部分还是基于 Flex.Silverlight 甚至 Applet 这样的 ...

  5. 基于 HTML5 结合互联网+的电力接线图

    前言 “互联网+”思维让数据的搜集和获取更加便捷,并且随着大数据的深度开发和应用,数据分析预测对于提升用户体验有非常重要的价值,同时也为不同行业.不同领域的合作提供了更广阔的空间.传统的发电企业是一个 ...

  6. android黑科技——完美解决界面逻辑的数据框架DataBinding(最新)的使用(二)

    昨天我们一起学习了dataBinding的基础用法,我想你可能还停留在它只是不用再findViewById,其实不然,今天我们就来扩展延伸,看看这个框架到底有什么魔力让谷歌官方大力推崇.这里还没看昨天 ...

  7. Debug Databinding Issues in WPF

    DataBinding is one of the most powerful features in WPF. But because it resolves the bindings at run ...

  8. Google官方关于Android架构中MVP模式的示例续-DataBinding

    基于前面的TODO示例,使用Data Binding库来显示数据并绑定UI元素的响应动作. 这个示例并未严格遵循 Model-View-ViewModel 或 Model-View-Presenter ...

  9. Js: Extensible Calendar Examples

    http://ext.ensible.comhttps://github.com/bmoeskau/Extensiblehttps://github.com/TeamupCom/extensibleh ...

随机推荐

  1. c++ 类静态成员、非静态成员初始化

    1.静态成员初始化(不能在构造函数或初始化列表中初始化) 1.1 所有静态成员都可以在类定义之外初始化(通用),如下所示 class test { public: static int a; }; ; ...

  2. html5新增标签及兼容

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. angularjs向后台传参,后台收不到数据

    angularjs中封装了一个$http服务,用来请求远程资源 参见:HTTP API 其中封装过的$http.post和$http.get使用起来比较方便 后台是php,用$_POST['name' ...

  4. R平方

    参考其他网页 通常R2越大越好,但看到亦在后面标上P值,这两者之间有何联系? R2和p值没有必然联系.就像你做线性分析和(单因素或多因素)方差分析一样,若A和K线性相关,也有可能A对K么有显著性影响一 ...

  5. Windows下安装Redis

    1.首先,Redis官方是支持Linux系统的,我这里不多说,需要的可以参考:http://www.oschina.net/question/12_18065/ 2.Windows 64位下载地址:h ...

  6. Swift - UITableView里的cell底部分割线左侧靠边

    override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, ...

  7. 基于GMap.Net的地图解决方案

    一 地图的加载与显示 关于GMap的介绍与使用可以看我以前的文章: GMap.Net开发之在WinForm和WPF中使用GMap.Net地图插件 GMap.Net是.Net下一个地图控件,可以基于Ht ...

  8. 高性能PHP框架thinkphp5.0.0 Beta发布-为API开发而设计

    ThinkPHP V5.——为API开发而设计的高性能框架 ThinkPHP5..0版本是一个颠覆和重构版本,采用全新的架构思想,引入了很多的PHP新特性,优化了核心,减少了依赖,实现了真正的惰性加载 ...

  9. 图文介绍如何在Eclipse统计代码行数

    使用Eclipse可以方便的统计工程或文件的代码行数,方法如下: 1.点击要统计的项目或许文件夹,在菜单栏点击Search,然后点击File... 2.选中正则表达式(Regular expressi ...

  10. DevExpress的GridControl的实时加载数据解决方案(取代分页)

    http://blog.csdn.net/educast/article/details/4769457 evExpress是一套第三方控件 其中有类似DataGridView的控件 今天把针对Dev ...