WinForm之BindingSource基礎(chǔ)操作實例教程
通常我們在進行數(shù)據(jù)綁定的時候,常用的數(shù)據(jù)源有DataSet、DataTable、BindingList<T>、還有強類型數(shù)據(jù)源。今天我們來通過實例了解一下BindingSource組建,分享給大家供大家參考借鑒之用。
BindingSource的兩個用途:
(1)首先,它提供一個將窗體上的控件綁定到數(shù)據(jù)的間接層。這是通過將 BindingSource 組件綁定到數(shù)據(jù)源,然后將窗體上的控件綁定到 BindingSource 組件來完成的。與數(shù)據(jù)的所有進一步交互(包括導(dǎo)航、排序、篩選和更新)都是通過調(diào)用 BindingSource 組件來完成的。
(2)其次,BindingSource 組件可以充當(dāng)強類型數(shù)據(jù)源。使用 Add 方法向 BindingSource 組件添加類型會創(chuàng)建一個該類型的列表。
一、對BindingSource的基礎(chǔ)操作——增刪改查
實例代碼如下:
public partial class Form1 : Form { //注當(dāng)前DGV已經(jīng)綁定到 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()); } //獲取當(dāng)前項 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); } //修改當(dāng)前項 private void button7_Click(object sender, EventArgs e) { Custom custom = (Custom)this.source.Current; custom.Name = "修改后的值"; this.source.ResetCurrentItem(); } //刪除當(dāng)前項 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 值。
第一種情況演示如何設(shè)置字符串屬性的 NullValue;第二種情況演示如何設(shè)置圖像屬性的 NullValue。
下面的示例演示如何在兩種不同情況下綁定 DBNull 值。第一種情況演示如何設(shè)置字符串屬性的 NullValue;第二種情況演示如何設(shè)置圖像屬性的 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#程序設(shè)計的學(xué)習(xí)有所幫助!
- c# 重載WndProc,實現(xiàn)重寫“最小化”的實現(xiàn)方法
- C#中Winform窗體Form的關(guān)閉按鈕變灰色的方法
- C# Winform實現(xiàn)捕獲窗體最小化、最大化、關(guān)閉按鈕事件的方法
- WinForm實現(xiàn)跨進程通信的方法
- C#之WinForm WebBrowser實用技巧匯總
- Winform下實現(xiàn)圖片切換特效的方法
- C#實現(xiàn)WinForm捕獲最小化事件的方法
- WinForm中變Enter鍵為Tab鍵實現(xiàn)焦點轉(zhuǎn)移的方法
- WinForm中的幾個實用技巧匯總
- WinForm相對路徑的陷阱
- WinForm中DefWndProc、WndProc與IMessageFilter的區(qū)別
相關(guān)文章
C# Socket編程實現(xiàn)簡單的局域網(wǎng)聊天器的示例代碼
這篇文章主要介紹了C# Socket編程實現(xiàn)簡單的局域網(wǎng)聊天器,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03C#實現(xiàn)漢字轉(zhuǎn)拼音(多音字)功能詳解
這篇文章主要為大家詳細介紹了如何利用C#實現(xiàn)漢字轉(zhuǎn)拼音(支持多音字)的功能,文中的示例代碼講解詳細,對我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2023-02-02使用aspose.word 第三方的插件實現(xiàn)導(dǎo)出word
本文給大家分享的是一個使用使用aspose.word 第三方的插件實現(xiàn)導(dǎo)出word的實例,十分的實用,有需要的小伙伴可以參考下。2015-06-06Unity輸出帶點擊跳轉(zhuǎn)功能的Log實現(xiàn)技巧詳解
這篇文章主要為大家介紹了Unity輸出帶點擊跳轉(zhuǎn)功能的Log實現(xiàn)技巧詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11利用windows性能計數(shù)器進行服務(wù)器性能監(jiān)控示例分享
這篇文章主要介紹了利用windows性能計數(shù)器進行服務(wù)器性能監(jiān)控的方法,大家可以參考擴展其它功能2014-01-01