-
C#教程之WinForm之BindingSource基础操作实例教程
通常我们在进行数据绑定的时候,常用的数据源有DataSet、DataTable、BindingList<T>、还有强类型数据源。今天我们来通过实例了解一下BindingSource组建,分享给大家供大家参考借鉴之用。
BindingSource的两个用途:
(1)首先,它提供一个将窗体上的控件绑定到数据的间接层。这是通过将 BindingSource 组件绑定到数据源,然后将窗体上的控件绑定到 BindingSource 组件来完成的。与数据的所有进一步交互(包括导航、排序、筛选和更新)都是通过调用 BindingSource 组件来完成的。
(2)其次,BindingSource 组件可以充当强类型数据源。使用 Add 方法向 BindingSource 组件添加类型会创建一个该类型的列表。
一、对BindingSource的基础操作——增删改查
实例代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
public partial class Form1 : Form { //注当前DGV已经绑定到 ID 和 Name 列 private BindingSource source = new BindingSource(); public Form1() { InitializeComponent(); } //窗体加载 private void Form1_Load( object sender, EventArgs e) { this .source.DataSource = typeof (Custom); this .dataGridView1.DataSource = this .source; } //添加 private void button1_Click( object sender, EventArgs e) { this .source.Add( new Custom(1, "A" )); this .source.Add( new Custom(2, "B" )); } //删除 private void button2_Click( object sender, EventArgs e) { this .source.RemoveAt(0); } //排序 【有问题】 private void button3_Click( object sender, EventArgs e) { this .source.Sort = "ID ASC" ; this .source.ResetBindings( false ); } //筛选 【有问题】 private void button4_Click( object sender, EventArgs e) { this .source.Filter = "ID = 1" ; this .source.ResetBindings( false ); } //向下移动 private void button5_Click( object sender, EventArgs e) { this .source.MoveNext(); MessageBox.Show( this .source.Position.ToString()); } //向上移动 private void button9_Click( object sender, EventArgs e) { this .source.MovePrevious(); MessageBox.Show( this .source.Position.ToString()); } //获取当前项 private void button6_Click( object sender, EventArgs e) { Custom custom = (Custom) this .source.Current; MessageBox.Show( " 所处的位置 : " + this .source.IndexOf(custom).ToString()); MessageBox.Show( "custom.Name : " + custom.Name); } //修改当前项 private void button7_Click( object sender, EventArgs e) { Custom custom = (Custom) this .source.Current; custom.Name = "修改后的值" ; this .source.ResetCurrentItem(); } //删除当前项 private void button8_Click( object sender, EventArgs e) { Custom custom = (Custom) this .source.Current; this .source.Remove(custom); } } //自定义类 字段必须属性公开化 public class Custom { public Custom() { } public Custom( int ID, string Name) { this .ID = ID; this .Name = Name; } private int id; public int ID { get { return id; } set { id = value; } } private string name; public string Name { get { return name; } set { name = value; } } } |
二、 下面的示例演示如何在两种不同情况下绑定 DBNull 值。
第一种情况演示如何设置字符串属性的 NullValue;第二种情况演示如何设置图像属性的 NullValue。
下面的示例演示如何在两种不同情况下绑定 DBNull 值。第一种情况演示如何设置字符串属性的 NullValue;第二种情况演示如何设置图像属性的 NullValue。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
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()); } } } |
希望本文实例对大家C#程序设计的学习有所帮助!
栏目列表
最新更新
python爬虫及其可视化
使用python爬取豆瓣电影短评评论内容
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
uniapp/H5 获取手机桌面壁纸 (静态壁纸)
[前端] DNS解析与优化
为什么在js中需要添加addEventListener()?
JS模块化系统
js通过Object.defineProperty() 定义和控制对象
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比