aparche 通过本文主要向大家介绍了c#databindings,databindings,databindings.add,winform databindings,c label.databindings等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
本文实例讲述了C#中DataBindings用法。分享给大家供大家参考,具体如下:
在C#操作数据库过程中,针对一般的文本控件,比如TextBox,Label等,我们赋值直接使用类似TextBox.Text=****的方式来进行,这种方式从某种意义上来说的确是最简便的方式,但是对于复杂一些的空间,比如说DataGridView,这个时候,绑定数据源我们一般使用DataGridView1.DataSource=****的方式来进行,如果数据源稍微有更改,那么只需要重新调用绑定一遍即可。可以说这种方式是单向的,也即从数据库到UI,但是有没有一种方式能够实现数据源改变的时候,不用重新绑定DataGridView就让它能够自动刷新数据呢,当然,这里要提到的就是DataBinding了。
代码如下
Form2.cs代码:
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; namespace DataBindingsTest { public partial class Form2 : Form { public Form2() { InitializeComponent(); } MyDataSource mydatasource = new MyDataSource(); //应用于第二种方式 public int Num { get; set; } //应用于第三种方式 public List<BlogNew> blogNews { get; set; } //应用于第四种方式 public BindingList<BlogNew> blogNewsRegardUI { get; set; } //应用于DataGridView界面UI更新 private void mainFrm_Load(object sender, EventArgs e) { #region 测试一 /************************************************ * 第一个值:要绑定到TextBox的什么地方 * 第二个值:数据源是什么 * 第三个值:应该取数据源的什么属性 * 第四个值:是否开启数据格式化 * 第五个值:在什么时候启用数据源绑定 * *********************************************/ textBox1.DataBindings.Add("Text", trackBar1, "Value", false, DataSourceUpdateMode.OnPropertyChanged); #endregion #region 测试二 /********************************************* * 这个主要就是通过一个外部的类,当做数据源 * *********************************************/ mydatasource.Myvalue = "这是个测试"; textBox2.DataBindings.Add("Text", mydatasource, "Myvalue", false, DataSourceUpdateMode.OnPropertyChanged); #endregion #region 测试三 /***************************************** *这个主要就是通过本身拥有的属性,当做数据源 ****************************************/ Num = 5; textBox3.DataBindings.Add("Text", this, "Num", false, DataSourceUpdateMode.OnPropertyChanged); #endregion /* * 注意:上面的3个测试,改变文本框中的值,数据源中对应的属性值会改变 * 但是,数据源的属性值改变了,文本框中的值不会改变 */ #region 测试四 : List<T> blogNews = new List<BlogNew>(); blogNews.Add(new BlogNew { BlogID = 1, BlogTitle = "人生若只如初见" }); blogNews.Add(new BlogNew { BlogID = 2, BlogTitle = "何事秋风悲画扇" }); blogNews.Add(new BlogNew { BlogID = 3, BlogTitle = "最喜欢纳兰性德" }); dataGridView1.DataBindings.Add("DataSource", this, "blogNews", false, DataSourceUpdateMode.OnPropertyChanged); #endregion #region 测试五 : BindingList<T> blogNewsRegardUI = new BindingList<BlogNew>(); blogNewsRegardUI.Add(new BlogNew { BlogID = 11, BlogTitle = "僵卧孤村不自哀" }); blogNewsRegardUI.Add(new BlogNew { BlogID = 12, BlogTitle = "尚思为国戍轮台" }); blogNewsRegardUI.Add(new BlogNew { BlogID = 13, BlogTitle = "夜阑卧听风吹雨" }); dataGridView2.DataBindings.Add("DataSource", this, "blogNewsRegardUI", false, DataSourceUpdateMode.OnPropertyChanged); #endregion } private void button1_Click(object sender, EventArgs e) { //从这里可以看出,改变了TextBox2中的值,这里的值也改变了,原因是因为类属于引用类型 MessageBox.Show(mydatasource.Myvalue); } private void button2_Click(object sender, EventArgs e) { //从这里可以看出,改变了TextBox3中的值,这里的值也改变了, //原因是Num被当做了当前窗体的一个属性(窗体本身就是一个类),也属于引用类型 MessageBox.Show(Num.ToString()); //this.Num = 10; //MessageBox.Show(Num.ToString()); } private void button3_Click(object sender, EventArgs e) { //在这里向DataGridView中插入一行 var data = dataGridView1.DataSource as List<BlogNew>; data.Add(new BlogNew { BlogID = 4, BlogTitle = "取次花丛懒回顾,半缘修道半缘君" }); foreach (BlogNew blogNew in dataGridView1.DataSource as List<BlogNew>) { /*********** * 当我们心插入一条BlogID记录为4的数据的时候,在界面上可以看出dataGridView1的dataSource已经被更新, * 但是界面上依旧显示为BlogID为1,2,3三条数据,很奇怪 * *********************/ MessageBox.Show(blogNew.BlogID + "--" + blogNew.BlogTitle); } } private void button4_Click(object sender, EventArgs e) { /*这里主要用来解决DataGridView1界面不更新的问题,其实原因在于使用了List<BlogNew>,这里我们采用BindList<BlogNew> *通过测试,我们发现,只要数据源改变,界面就可以自动的进行更新了,很是方便,不需要重新绑定 */ var dataRegardUI = dataGridView2.DataSource as BindingList<BlogNew>; dataRegardUI.Add(new BlogNew { BlogID = 20, BlogTitle = "竹外桃花三两枝,春江水暖鸭先知" }); } } public class MyDataSource { public string Myvalue { get; set; } } public class BlogNew { public int BlogID { get; set; } public string BlogTitle { get; set; } } }</div>
Form2.Designer.cs代码:
namespace DataBindingsTest { partial class Form2 { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.groupBox1 = new System.Windows.Forms.GroupBox(); this.groupBox2 = new System.Windows.Forms.GroupBox(); this.button1 = new System.Windows.Forms.Button(); this.textBox2 = new System.Windows.Forms.TextBox(); this.groupBox3 = new System.Windows.Forms.GroupBox(); this.button2 = new System.Windows.Forms.Button(); this.textBox3 = new System.Windows.Forms.TextBox(); this.groupBox4 = new System.Windows.Forms.GroupBox(); this.button3 = new System.Windows.Forms.Button(); this.dataGridView1 = new System.Windows.Forms.DataGridView(); this.groupBox5 = new System.Windows.Forms.GroupBox(); this.button4 = new System.Windows.Forms.Button(); this.dataGridView2 = new System.Windows.Forms.DataGridView(); this.textBox1 = new System.Windows.Forms.TextBox(); this.trackBar1 = new System.Windows.Forms.TrackBar(); this.groupBox1.SuspendLayout(); this.groupBox2.SuspendLayout(); this.groupBox3.SuspendLayout(); this.groupBox4.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit(); this.groupBox5.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView2)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.trackBar1)).BeginInit(); this.SuspendLayout(); // // groupBox1 //