最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

WinForm之BindingSource基礎(chǔ)操作實(shí)例教程

 更新時(shí)間:2014年08月26日 12:11:34   投稿:shichen2014  
這篇文章主要介紹了WinForm之BindingSource基礎(chǔ)操作,對(duì)BindingSource組建的用法進(jìn)行較為深入的實(shí)例分析,需要的朋友可以參考下

通常我們?cè)谶M(jìn)行數(shù)據(jù)綁定的時(shí)候,常用的數(shù)據(jù)源有DataSet、DataTable、BindingList<T>、還有強(qiáng)類型數(shù)據(jù)源。今天我們來(lái)通過(guò)實(shí)例了解一下BindingSource組建,分享給大家供大家參考借鑒之用。

BindingSource的兩個(gè)用途:

(1)首先,它提供一個(gè)將窗體上的控件綁定到數(shù)據(jù)的間接層。這是通過(guò)將 BindingSource 組件綁定到數(shù)據(jù)源,然后將窗體上的控件綁定到 BindingSource 組件來(lái)完成的。與數(shù)據(jù)的所有進(jìn)一步交互(包括導(dǎo)航、排序、篩選和更新)都是通過(guò)調(diào)用 BindingSource 組件來(lái)完成的。
(2)其次,BindingSource 組件可以充當(dāng)強(qiáng)類型數(shù)據(jù)源。使用 Add 方法向 BindingSource 組件添加類型會(huì)創(chuàng)建一個(gè)該類型的列表。

一、對(duì)BindingSource的基礎(chǔ)操作——增刪改查

實(shí)例代碼如下:

  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);
    }
    //排序 【有問(wèn)題】
    private void button3_Click(object sender, EventArgs e)
    {
      this.source.Sort = "ID ASC";
      this.source.ResetBindings(false);
    }
    //篩選 【有問(wèn)題】
    private void button4_Click(object sender, EventArgs e)
    {
      this.source.Filter = "ID = 1";
      this.source.ResetBindings(false);
    }
    //向下移動(dòng)
    private void button5_Click(object sender, EventArgs e)
    {
      this.source.MoveNext();
      MessageBox.Show(this.source.Position.ToString());
    }
    //向上移動(dòng)
    private void button9_Click(object sender, EventArgs e)
    {
      this.source.MovePrevious();
      MessageBox.Show(this.source.Position.ToString());
    }
    //獲取當(dāng)前項(xià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)前項(xiàng)
    private void button7_Click(object sender, EventArgs e)
    {
      Custom custom = (Custom)this.source.Current;
      custom.Name = "修改后的值";
      this.source.ResetCurrentItem();
    }
    //刪除當(dāng)前項(xià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());
    }
  }
}

希望本文實(shí)例對(duì)大家C#程序設(shè)計(jì)的學(xué)習(xí)有所幫助!

相關(guān)文章

  • C# 動(dòng)態(tài)輸出Dos命令執(zhí)行結(jié)果的實(shí)例(附源碼)

    C# 動(dòng)態(tài)輸出Dos命令執(zhí)行結(jié)果的實(shí)例(附源碼)

    這篇文章主要介紹了C# 動(dòng)態(tài)輸出Dos命令執(zhí)行結(jié)果的實(shí)例,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • Unity中的InitializeOnLoad特性實(shí)踐深入解析

    Unity中的InitializeOnLoad特性實(shí)踐深入解析

    這篇文章主要為大家介紹了Unity中的InitializeOnLoad特性實(shí)踐深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-05-05
  • C#中通過(guò)LRU實(shí)現(xiàn)通用高效的超時(shí)連接探測(cè)

    C#中通過(guò)LRU實(shí)現(xiàn)通用高效的超時(shí)連接探測(cè)

    這篇文章主要介紹了c#中通過(guò)LRU實(shí)現(xiàn)通用高效的超時(shí)連接探測(cè),非常不錯(cuò),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下
    2018-11-11
  • C#實(shí)現(xiàn)把彩色圖片灰度化代碼分享

    C#實(shí)現(xiàn)把彩色圖片灰度化代碼分享

    這篇文章主要介紹了C#實(shí)現(xiàn)把彩色圖片灰度化代碼分享,用在一些特殊場(chǎng)合中,需要的朋友可以參考下
    2014-08-08
  • C#8.0中的模式匹配

    C#8.0中的模式匹配

    這篇文章介紹了C#8.0中的模式匹配,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-07-07
  • C#實(shí)現(xiàn)逐行讀取和寫入文件的方法

    C#實(shí)現(xiàn)逐行讀取和寫入文件的方法

    這篇文章給大家介紹了使用C#語(yǔ)言實(shí)現(xiàn)同樣的功能,即從輸入文件中讀取每行數(shù)據(jù),然后將每行字段組合成SQL插入腳本,然后逐行寫入另外一個(gè)空白文件中,感興趣的朋友可以參考下
    2024-01-01
  • 詳解c# 線程同步

    詳解c# 線程同步

    這篇文章主要介紹了c# 線程同步的相關(guān)資料,文中講解非常細(xì)致,示例代碼幫助大家更好的理解和學(xué)習(xí)c# 多線程,感興趣的朋友可以了解下
    2020-07-07
  • 基于C#實(shí)現(xiàn)的HOOK鍵盤鉤子實(shí)例代碼

    基于C#實(shí)現(xiàn)的HOOK鍵盤鉤子實(shí)例代碼

    這篇文章主要介紹了基于C#實(shí)現(xiàn)的HOOK鍵盤鉤子實(shí)例,需要的朋友可以參考下
    2014-07-07
  • C#?Chart?簡(jiǎn)單使用教程

    C#?Chart?簡(jiǎn)單使用教程

    Chart控件可以用來(lái)繪制波形圖、柱狀圖、餅圖、折線圖等,用來(lái)進(jìn)行數(shù)據(jù)表現(xiàn)是很不錯(cuò)的,現(xiàn)在簡(jiǎn)單說(shuō)一下這個(gè)控件的使用方法,對(duì)C#?Chart使用相關(guān)知識(shí)感興趣的朋友一起看看吧
    2022-11-11
  • 在GridControl控件上綁定圖片的幾種操作方式詳解

    在GridControl控件上綁定圖片的幾種操作方式詳解

    GridControl控件是經(jīng)常用來(lái)綁定數(shù)據(jù)的,一般以常規(guī)的字符內(nèi)容為主,有時(shí)候也會(huì)有圖片的顯示需要,這篇文章主要介紹了在GridControl控件上綁定圖片的幾種操作方式詳解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-07-07

最新評(píng)論

定襄县| 宣化县| 阳曲县| 罗甸县| 蓬莱市| 芦溪县| 合川市| 罗甸县| 布拖县| 庆城县| 银川市| 泸州市| 抚远县| 黄浦区| 沂南县| 武清区| 贵阳市| 达州市| 英山县| 普兰店市| 阿瓦提县| 长沙市| 长兴县| 台湾省| 日照市| 嘉黎县| 无为县| 宁远县| 伊宁县| 天津市| 顺平县| 临沭县| 鹤山市| 巴楚县| 临潭县| 苏尼特右旗| 邯郸市| 惠东县| 金溪县| 新乡县| 黎川县|