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 ...
随机推荐
- 在vs中char类型的实参与LPCWSTR类型的形参类型不兼容怎么解决?
今天在做 COS脚本解释器的时候,遇到了这个问题 先了解一下 LPCTCHAR 这个东东 LPCTSTR用来表示你的字符是否使用UNICODE, 如果你的程序定义了UNICODE或者其他相关的宏,那么 ...
- TraceView进行性能分析
一.TraceView概述 TraceView 是 Android 平台配备一个很好的性能分析的工具.它可以通过图形化的方式让我们了解我们要跟踪的程序的性能,并且能具体到 method. 详细内容参考 ...
- C#基本工具代码
1.下载Xlsx public static void TryToDisplayGeneratedFileXlsx(string writeFilePath, string fileName) { H ...
- 开发基于Edge渲染内核的浏览器应用
在使用Edge之前,我们先来看看UWP(Universal Windows Platform)平台.微软研发了多种设备平板.手机.Xbox.个人电脑等,在此之前,如果需要给每台设备开发程序,都需要对应 ...
- Path
<Path Data="M17.5,16.5 L17.5,18.5 19.5,18.5 19.5,16.5 z M11.5,16.5 L11.5,18.5 13.5,18.5 13.5 ...
- JS和JQuery的总结
JS部分 一, 词法结构 区分大小 注意://单行 /*多行注释*/ 字面量(直接量literal) 12 // 数字 5.8//小数 "hello" 'hello' true ...
- Redis的缓存策略和主键失效机制
作为缓存系统都要定期清理无效数据,就需要一个主键失效和淘汰策略. >>EXPIRE主键失效机制 在Redis当中,有生存期的key被称为volatile,在创建缓存时,要为给定的key设置 ...
- Pyqt 获取打包二进制文件中的资源
记得有一次打开一个单独exe程序,点击btn中的一个帮助说明按钮,在同级目录下就多出一个help.chm 文件并自动打开. 那这个exe肯定是把help.chm 打包到exe中,当我触发“帮助”按钮的 ...
- Logistic回归的使用
Logistic回归的使用和缺失值的处理 从疝气病预测病马的死亡率 数据集: UCI上的数据,368个样本,28个特征 测试方法: 交叉测试 实现细节: 1.数据中因为存在缺失值所以要进行预处理,这点 ...
- WPF
最近在学习WPF,学习WPF首先上的是微软的MSDN,然后再搜索了一下网络有关WPF的学习资料.为了温故而知新把学习过程记录下来,以备后查.这篇主要讲WPF的开发基础,介绍了如何使用Visual St ...