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. git workflow

    1) fork map-matcher.git repo 2) add ssh-keygen public key to gitlab 3) clone repo git clone git@git. ...

  2. win8环境安装.net3.5

    材料:光盘镜像(必须原版镜像) 1,以管理员身份运行CMD 2,打开镜像,找到盘符比如我的是G盘 3, 输入 X代表你的ISO镜像的盘符 dism.exe /online /enable-featur ...

  3. strust1与strust2,springmvc三者之间的区别?

    strust1与struts2的区别    1.struts2是基于webWork的一个全新的框架,比struts1学习更方便    Struts2主要是改进了Struts1的servlet和acti ...

  4. 【XLL 框架库函数】 QuitFramework

    去初使化框架库,简问题是才的重新初使化 XLOPER/XLOPER12. 参数 这个函数没有参数 属性值/返回值 这个函数没有返回值.

  5. MVC 前台获取三级菜单及子菜单

    1.在后台将所有的菜单获取出来,代码如下: public ActionResult Index() { //所有商品分类 var oneMenu = _baseGoodsCategory.FindLi ...

  6. php 总结第一篇(望大家补充!谢谢)

    /* 数组的常用函数 * * 数组的排序函数 *   sort() *   rsort() *   usort() *   asort() *   arsort() *   uasort() *   ...

  7. sqlserver索引小结

    1.1 什么是索引? SQL索引有两种,聚集索引和非聚集索引,索引主要目的是提高了SQL Server系统的性能,加快数据的查询速度与减少系统的响应时间 下面举两个简单的例子: 图书馆的例子:一个图书 ...

  8. HTML5本地存储——Web SQL Database

    在HTML5 WebStorage介绍了html5本地存储的Local Storage和Session Storage,这两个是以键值对存储的解决方案,存储少量数据结构很有用,但是对于大量结构化数据就 ...

  9. [LeetCode] Best Time to Buy and Sell Stock

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  10. [Linux]在linux中,常常用到ctrl和其他按键组合,常用的有哪些及意义呢

    在linux中,常常用到ctrl和其他按键组合,常用的有哪些及意义呢? Ctrl+c 结束正在运行的程序 Ctrl+d 结束输入或退出shell Ctrl+s 暂停屏幕输出[锁住终端] Ctrl+q ...