《(学习笔记)两天进步一点点》(2) ——BindingSource基础操作
2010-08-12 01:21
549 查看
以前在进行数据绑定的时候,通常用的数据源有DataSet、DataTable、BindingList<T>、还有强类型数据源。今天呢、我看了一下BindingSource组建,感觉还不错特将学习笔记分大家分享。
一、BindingSource的两个用途
(1)首先,它提供一个将窗体上的控件绑定到数据的间接层。这是通过将 BindingSource 组件绑定到数据源,然后将窗体上的控件绑定到 BindingSource 组件来完成的。与数据的所有进一步交互(包括导航、排序、筛选和更新)都是通过调用 BindingSource 组件来完成的。
(2)其次,BindingSource 组件可以充当强类型数据源。使用 Add 方法向 BindingSource 组件添加类型会创建一个该类型的列表。
一、对BindingSource的基础操作——增删改查
绑定 DBNull 值
《(学习笔记)两天进步一点点》持续更新中… 望大侠们指点一二,谢谢!
一、BindingSource的两个用途
(1)首先,它提供一个将窗体上的控件绑定到数据的间接层。这是通过将 BindingSource 组件绑定到数据源,然后将窗体上的控件绑定到 BindingSource 组件来完成的。与数据的所有进一步交互(包括导航、排序、筛选和更新)都是通过调用 BindingSource 组件来完成的。
(2)其次,BindingSource 组件可以充当强类型数据源。使用 Add 方法向 BindingSource 组件添加类型会创建一个该类型的列表。
一、对BindingSource的基础操作——增删改查
绑定 DBNull 值
//*********************************************************************** 下面的示例演示如何在两种不同情况下绑定 DBNull 值。第一种情况演示如何设置字符串属性的 NullValue;第二种情况演示如何设置图像属性的 NullValue。 //*********************************************************************** using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Data.SqlClient; using System.Windows.Forms; namespace DBNullCS { public class Form1 : Form { public Form1() { this.Load += new EventHandler(Form1_Load); } // The controls and components we need for the form. private Button button1; private PictureBox pictureBox1; private BindingSource bindingSource1; private TextBox textBox1; private TextBox textBox2; // Data table to hold the database data. DataTable employeeTable = new DataTable(); void Form1_Load(object sender, EventArgs e) { // Basic form setup. this.pictureBox1 = new PictureBox(); this.bindingSource1 = new BindingSource(); this.textBox1 = new TextBox(); this.textBox2 = new TextBox(); this.button1 = new Button(); this.pictureBox1.Location = new System.Drawing.Point(20, 20); this.pictureBox1.Size = new System.Drawing.Size(174, 179); this.textBox1.Location = new System.Drawing.Point(25, 215); this.textBox1.ReadOnly = true; this.textBox2.Location = new System.Drawing.Point(25, 241); this.textBox2.ReadOnly = true; this.button1.Location = new System.Drawing.Point(200, 103); this.button1.Text = "Move Next"; this.button1.Click += new System.EventHandler(this.button1_Click); this.ClientSize = new System.Drawing.Size(292, 273); this.Controls.Add(this.button1); this.Controls.Add(this.textBox2); this.Controls.Add(this.textBox1); this.Controls.Add(this.pictureBox1); this.ResumeLayout(false); this.PerformLayout(); // Create the connection string and populate the data table // with data. string connectionString = "Integrated Security=SSPI;" + "Persist Security Info = False;Initial Catalog=Northwind;" + "Data Source = localhost"; SqlConnection connection = new SqlConnection(); connection.ConnectionString = connectionString; SqlDataAdapter employeeAdapter = new SqlDataAdapter(new SqlCommand("Select * from Employees", connection)); connection.Open(); employeeAdapter.Fill(employeeTable); // Set the DataSource property of the BindingSource to the employee table. bindingSource1.DataSource = employeeTable; // Set up the binding to the ReportsTo column. Binding reportsToBinding = textBox2.DataBindings.Add("Text", bindingSource1, "ReportsTo", true); // Set the NullValue property for this binding. reportsToBinding.NullValue = "No Manager"; // Set up the binding for the PictureBox using the Add method, setting // the null value in method call. pictureBox1.DataBindings.Add("Image", bindingSource1, "Photo", true, DataSourceUpdateMode.Never, new Bitmap(typeof(Button), "Button.bmp")); // Set up the remaining binding. textBox1.DataBindings.Add("Text", bindingSource1, "LastName", true); } // Move through the data when the button is clicked. private void button1_Click(object sender, EventArgs e) { bindingSource1.MoveNext(); } [STAThread] static void Main() { Application.EnableVisualStyles(); Application.Run(new Form1()); } } }
《(学习笔记)两天进步一点点》持续更新中… 望大侠们指点一二,谢谢!
相关文章推荐
- 《(学习笔记)两天进步一点点》——三行代码
- 《(学习笔记)两天进步一点点》(1)——Windows控件DGV
- 《(学习笔记)两天进步一点点》——ListView AND TreeVie
- 《(学习笔记)两天进步一点点》(3)——应用BindingSource实现数据同步
- 《(学习笔记)两天进步一点点》(5)——几个比较小的类,但很实用
- 学习笔记:基础+文件操作(上传|下载)+面向对象+xml
- linux基础学习笔记——操作
- 学习javaScript的部分笔记,自己第一次写东西就当是个日记啦 一天进步一点点
- Java基础学习笔记之XML文件操作
- HTML&CSS基础学习笔记6-文本操作标签
- jQuery学习笔记之基础DOM和CSS操作
- [java学习笔记]java语言基础概述之数组的定义&常见操作(遍历、排序、查找)&二维数组
- .Net学习笔记8-.Net基础2 IO操作
- [学习笔记]JavaScript基础--数组操作
- “黑马程序员”视频学习笔记之dom操作基础
- 黑马程序员--Java基础学习笔记【文件操作、递归】
- 学习笔记之javaweb基础:jsp基本语法,内置对象,对JavaBean的操作语法
- 学习笔记_oracle——基础操作
- MySQL 学习基础笔记-MySQL基本数据库的操作
- Swift学习笔记(十六)——String字符串的基础操作