亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 編程 > C# > 正文

WinForm之BindingSource基礎操作實例教程

2020-01-24 02:28:55
字體:
來源:轉載
供稿:網友

通常我們在進行數據綁定的時候,常用的數據源有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#程序設計的學習有所幫助!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
欧美在线性视频| 91国产高清在线| 国产精品久久77777| 粉嫩老牛aⅴ一区二区三区| 3344国产精品免费看| 成人黄色av网站| 一本色道久久综合狠狠躁篇的优点| 国产一区二区三区在线看| 日韩视频免费大全中文字幕| 亚洲第一中文字幕在线观看| 96pao国产成视频永久免费| 日韩欧美国产高清91| 久久免费视频这里只有精品| 日本精品一区二区三区在线播放视频| 亚洲久久久久久久久久| 久久精品91久久香蕉加勒比| 亚洲人成77777在线观看网| 欧美精品videos性欧美| 国产欧美在线看| 久久琪琪电影院| 精品久久久久久久久久ntr影视| 欧美性做爰毛片| 亚洲免费人成在线视频观看| 日韩精品亚洲元码| 亚洲国产成人在线播放| 亚洲综合色激情五月| 国产日韩在线亚洲字幕中文| 美女999久久久精品视频| 国产99久久精品一区二区 夜夜躁日日躁| 国产999视频| 国产成人精品日本亚洲专区61| 国产欧美在线视频| 国产91在线播放| 亚洲一区二区三区乱码aⅴ| 国产精品xxxxx| 视频在线观看99| 成人精品久久av网站| 亚洲精品成人久久| 国产精品亚洲美女av网站| 亚洲美女久久久| 成人字幕网zmw| 欧美在线免费看| 在线国产精品播放| 国产亚洲成精品久久| 亚洲www在线观看| 国产精品久久久久久久久久久不卡| 国产成+人+综合+亚洲欧美丁香花| 久久亚洲精品成人| 国产精品黄色影片导航在线观看| 亚洲精品国精品久久99热| 色一情一乱一区二区| 久久久久久久国产精品视频| 欧美日韩999| 国产美女扒开尿口久久久| 亚洲欧洲高清在线| 国产精品成人播放| 国产精品免费观看在线| 国产午夜精品全部视频在线播放| 欧美巨猛xxxx猛交黑人97人| 国外色69视频在线观看| 北条麻妃在线一区二区| 中文字幕亚洲一区二区三区| 久久婷婷国产麻豆91天堂| 中文字幕亚洲欧美| 中文字幕久久精品| 日韩一区视频在线| 国产香蕉一区二区三区在线视频| 久久亚洲一区二区三区四区五区高| 国产精品永久免费观看| 91po在线观看91精品国产性色| 久久精品国产99国产精品澳门| 亚洲欧美中文字幕| 国产成人精品电影久久久| 欧美成人免费一级人片100| 庆余年2免费日韩剧观看大牛| 亚洲欧美在线磁力| 欧美成人久久久| 国产精品日韩电影| 欧美成人中文字幕在线| 国产精品∨欧美精品v日韩精品| 国产精品第100页| 国产裸体写真av一区二区| 亚洲iv一区二区三区| 亚洲中国色老太| 国产精品久久久久久久久久三级| 欧美日韩在线视频一区| 在线观看欧美日韩国产| 国产91精品视频在线观看| 亚洲人成欧美中文字幕| 国产精品精品久久久久久| 国产精品久久久久久久av大片| 国产精品香蕉在线观看| 国产精品jizz在线观看麻豆| 91最新在线免费观看| 亚洲精品中文字幕有码专区| 日韩欧美国产激情| 538国产精品一区二区免费视频| 国产成人亚洲综合| 久久久久北条麻妃免费看| 亚洲一区免费网站| 麻豆乱码国产一区二区三区| 国产精品黄视频| 日韩美女av在线免费观看| 中文字幕一区日韩电影| 欧美激情国产日韩精品一区18| 成人免费视频xnxx.com| 最近中文字幕mv在线一区二区三区四区| www.日韩不卡电影av| 国产精品露脸av在线| 亚洲欧美第一页| 国产成人精品在线| 日韩欧美精品免费在线| 亚洲国产精品va在线看黑人动漫| 91免费欧美精品| 欧美最猛性xxxxx(亚洲精品)| 欧美视频13p| 久久久久久久久久久免费精品| 日韩亚洲精品电影| 动漫精品一区二区| 2018国产精品视频| 日韩av中文字幕在线免费观看| 日韩免费在线免费观看| 欧美激情网站在线观看| 懂色av中文一区二区三区天美| 亚洲电影天堂av| 日本久久久久久久久| 亚洲永久免费观看| 日韩在线国产精品| 性色av一区二区三区红粉影视| 欧美性猛交xxxxx水多| 中文字幕亚洲一区| 国产精品亚发布| 久久综合伊人77777尤物| 国产91色在线免费| 欧美成人一区二区三区电影| 日韩精品高清视频| 国产精品专区一| 亚洲一区二区三区乱码aⅴ| 日韩欧美国产免费播放| 亚洲欧美第一页| 久久久亚洲影院你懂的| 亚洲综合在线播放| 亚洲一区二区三区四区视频| 欧美成年人视频网站| 国产精品成人久久久久| 久久精品电影一区二区| 亚洲精品国产suv| 亚洲人在线视频| 亚洲福利影片在线| 欧美日韩国产成人高清视频| 国产精品va在线播放我和闺蜜| 日本精品久久久久影院| 日韩av网址在线| 亚洲精品视频网上网址在线观看| 一本大道亚洲视频| 久久久亚洲精品视频| 久久艹在线视频| 97视频在线免费观看| 国模精品一区二区三区色天香| 亚洲第一国产精品| 精品国产91久久久久久| 日韩一区二区欧美| 日韩高清不卡av| 国产伊人精品在线|