DataBinding examples
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的更多相关文章
- 数百个 HTML5 例子学习 HT 图形组件 – WebGL 3D 篇
<数百个 HTML5 例子学习 HT 图形组件 – 拓扑图篇>一文让读者了解了 HT的 2D 拓扑图组件使用,本文将对 HT 的 3D 功能做个综合性的介绍,以便初学者可快速上手使用 HT ...
- 数百个 HTML5 例子学习 HT 图形组件 – 拓扑图篇
HT 是啥:Everything you need to create cutting-edge 2D and 3D visualization. 这口号是当年心目中的产品方向,接着就朝这个方向慢慢打 ...
- HTML5 例子学习 HT 图形组件
HTML5 例子学习 HT 图形组件 HT 是啥:Everything you need to create cutting-edge 2D and 3D visualization. 这口号是当年心 ...
- 基于 HTML5 的电力接线图 SCADA 应用
在电力.油田燃气.供水管网等工业自动化领域 Web SCADA 的概念已经提出了多年,早些年的 Web SCADA 前端技术大部分还是基于 Flex.Silverlight 甚至 Applet 这样的 ...
- 基于 HTML5 结合互联网+的电力接线图
前言 “互联网+”思维让数据的搜集和获取更加便捷,并且随着大数据的深度开发和应用,数据分析预测对于提升用户体验有非常重要的价值,同时也为不同行业.不同领域的合作提供了更广阔的空间.传统的发电企业是一个 ...
- android黑科技——完美解决界面逻辑的数据框架DataBinding(最新)的使用(二)
昨天我们一起学习了dataBinding的基础用法,我想你可能还停留在它只是不用再findViewById,其实不然,今天我们就来扩展延伸,看看这个框架到底有什么魔力让谷歌官方大力推崇.这里还没看昨天 ...
- Debug Databinding Issues in WPF
DataBinding is one of the most powerful features in WPF. But because it resolves the bindings at run ...
- Google官方关于Android架构中MVP模式的示例续-DataBinding
基于前面的TODO示例,使用Data Binding库来显示数据并绑定UI元素的响应动作. 这个示例并未严格遵循 Model-View-ViewModel 或 Model-View-Presenter ...
- Js: Extensible Calendar Examples
http://ext.ensible.comhttps://github.com/bmoeskau/Extensiblehttps://github.com/TeamupCom/extensibleh ...
随机推荐
- PHP - xhprof+Graphviz 安装配置
简介:XHProf是Facebook放出的轻量级调试工具.和Xdebug相比,XHProf更加易用和可控,尤其是生成流程图和调试数据对比的功能很好很强大. 参考:http://us2.php.net/ ...
- HTTPS Web配置举例
http://www.h3c.com.cn/Products___Technology/Technology/Security_Encrypt/Other_technology/Representat ...
- ubuntu 安装vsftpd服务器 ftp
http://www.cnblogs.com/likwo/p/3154868.html http://help.aliyun.com/knowledge_detail.htm?knowledgeId= ...
- linux 使用 nvidia 的 gpu
第一种方法: [Wizard@Wizard ~]$ nvidia-detect kmod-nvidiaOptimus hardware detected: An Intel display contr ...
- go:匿名函数与闭包
一.匿名函数 定义:没有函数名的函数. 作用:在go语言中目前了解的作用就是用于构成闭包. *注:由于javascript不存在块级作用域,故匿名函数常用来包含代码以不污染全局命名空间,运行后销毁环境 ...
- yii2.0的分页和排序
//排序 $sort = new Sort([ 'attributes' => [ 'age' => [ 'asc' => ['age' => SORT_ASC], 'desc ...
- css垂直居中 两种方法
在前端面试的时候我们经常会被问道怎样使一个元素在页面垂直居中呢,这也是一个老生常谈的问题了. 解决的方法基本都是使用定位来实现 div{display: fixed;left: 50%;top: 50 ...
- hive创建索引
索引是hive0.7之后才有的功能,创建索引需要评估其合理性,因为创建索引也是要磁盘空间,维护起来也是需要代价的 创建索引 hive> create index [index_studentid ...
- Android之EACCES (Permission denied)与Permission denied异常探密
话说,Accipiter君,最近又开始怒学Android了,记得刚开始还是09年学的,现在的手机还是华为出的最早的一款Android手机C8500,那时候就想好好学习Android,赚点小钱,可是~~ ...
- [Liferay6.2.2]AUI的小坑:input的type属性
<aui:input name="name" label="姓名" value="<%=student.getName() %>&q ...