WinForm之BindingSource基础操作实例教程
通常我们在进行数据绑定的时候,常用的数据源有DataSet、DataTable、BindingList<T>、还有强类型数据源。今天我们来通过实例了解一下BindingSource组建,分享给大家供大家参考借鉴之用。
BindingSource的两个用途:
(1)首先,它提供一个将窗体上的控件绑定到数据的间接层。这是通过将 BindingSource 组件绑定到数据源,然后将窗体上的控件绑定到 BindingSource 组件来完成的。与数据的所有进一步交互(包括导航、排序、筛选和更新)都是通过调用 BindingSource 组件来完成的。
(2)其次,BindingSource 组件可以充当强类型数据源。使用 Add 方法向 BindingSource 组件添加类型会创建一个该类型的列表。
一、对BindingSource的基础操作——增删改查
实例代码如下:
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。
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#程序设计的学习有所帮助!
相关文章
- 本篇文章是对C#中WinForm控件之Dock顺序调整进行了详细的分析介绍,需要的朋友参考下...2020-06-25
- 这篇文章主要介绍了C# Winform中实现主窗口打开登录窗口关闭的方法,这在需要用户名密码的软件项目中是必用的一个技巧,要的朋友可以参考下...2020-06-25
- 下面小编就为大家带来一篇C# winform打开Excel文档的方法总结(必看篇)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
- 这篇文章主要给大家介绍C# winform快捷键设置技巧,涉及到C winform快捷键相关知识,对C winform知识感兴趣的朋友可以参考下本篇文章...2020-06-25
c#中Winform实现多线程异步更新UI(进度及状态信息)
本篇文章主要介绍了c#中Winform实现多线程异步更新UI(进度及状态信息) ,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25- 这篇文章主要介绍了C#中winform使用相对路径读取文件的方法,实例分析了C#使用相对路径读取文件的技巧与实际应用,需要的朋友可以参考下...2020-06-25
- 这篇文章主要给大家介绍了关于c# winform异步不卡界面的实现方法,文中通过示例代码介绍的非常详细,对大家学习或者使用c#具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧...2020-06-25
C# WinForm程序处理后台繁忙导致前台控件假死现象解决方法
这篇文章主要介绍了C# WinForm程序处理后台繁忙导致前台控件假死现象解决方法,本文通过Application.DoEvents()解决这个问题,并讲解了Application.DoEvents()的作用,需要的朋友可以参考下...2020-06-25winform中的ListBox和ComboBox绑定数据用法实例
这篇文章主要介绍了winform中的ListBox和ComboBox绑定数据用法,实例分析了将集合数据绑定到ListBox和ComboBox控件的技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25- 这篇文章主要介绍了C#在Winform开发中使用Grid++报表,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-25
- 这篇文章主要为大家详细介绍了C# WinForm程序设计简单计算器,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
C# WinForm控件对透明图片重叠时出现图片不透明的简单解决方法
这篇文章主要介绍了C# WinForm控件对透明图片重叠时出现图片不透明的简单解决方法,结合实例形式分析了WinForm图片重叠后造成图片不透明的原因与相应的解决方法,需要的朋友可以参考下...2020-06-25- 这篇文章主要介绍了C#实现Winform中打开网页页面的方法,涉及WinForm中WebBrowser的相关使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
- 在工作中遇到这样一个问题,在系统使用过程中,输入法会变灰导致无法使用输入法输入文字,就好像输入法被禁用了没有启用似的。对此,在这里做个备录...2020-06-25
C#实现winform中RichTextBox在指定光标位置插入图片的方法
这篇文章主要介绍了C#实现winform中RichTextBox在指定光标位置插入图片的方法,涉及RichTextBox控件及剪切板的相关操作技巧,非常简单实用,需要的朋友可以参考下...2020-06-25C# Winform 调用系统接口操作 INI 配置文件的代码
封装了一小段代码, 调用系统接口, 操作配置文件. 一般用于 .ini 文件, 或者其它键值对格式的配置文件...2020-06-25- 这篇文章主要介绍了WinForm调用百度地图接口用法,结合具体实例形式简单分析了WinForm WebBrower控件与前端百度接口交互的相关操作技巧,需要的朋友可以参考下...2020-06-25
- 在.NET应用中,导出Excel是很常见的需求,导出Excel报表大致有以下三种方式:Office PIA,文件流和NPOI开源库,本文只介绍前两种方式...2020-06-25
- 这篇文章主要介绍了C# WinForm中禁止改变窗口大小的方法,需要把FormBorderStyle和MaximizeBox的值固定即可,需要的朋友可以参考下...2020-06-25
- 这篇文章主要介绍了C# inForm编程获取文件物理路径的方法,获取的物理路径是软件即软件安装所在目录,需要的朋友可以参考下...2020-06-25