使用BindingSource組件將Windows Forms控件綁定到類型

https://docs.microsoft.com/en-us/dotnet/desktop/winforms/controls/how-to-bind-a-windows-forms-control-to-a-type?view=netframeworkdesktop-4.8windows

例子

下面的代碼經過使用BindingSource組件將DataGridView控件綁定到自定義類型。在運行示例時,您會注意到在用數據填充控件以前,DataGridView的標籤列反映了對象的屬性。該示例具備一個Add Customer按鈕,用於將數據添加到DataGridView控件。當您單擊按鈕時,新對象將添加到BindingSource中。在現實世界中,數據多是經過調用Web服務或其餘數據源得到的。api

BindingSource綁定到一個類型,而後能夠對這個BindingSource添加、刪除數據ide

using System;using System.Collections;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Data.SqlClient;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace WindowsFormsApplication2
{    public partial class Form1 : Form
    {
        BindingSource bSource = new BindingSource();        private Button button1;
        DataGridView dgv = new DataGridView();        public Form1()
        {            this.ClientSize = new System.Drawing.Size(362, 370);            this.button1 = new System.Windows.Forms.Button();            this.button1.Location = new System.Drawing.Point(140, 326);            this.button1.Name = "button1";            this.button1.AutoSize = true;            this.button1.Text = "Add Customer";            this.button1.Click += new System.EventHandler(this.button1_Click);            this.Controls.Add(this.button1);            // Set up the DataGridView control.
            dgv.Dock = DockStyle.Top;            this.Controls.Add(dgv);

            bSource.DataSource = typeof(DemoCustomer);
            dgv.DataSource = bSource;
        }        private void button1_Click(object sender, EventArgs e)
        {
            bSource.Add(new DemoCustomer(DateTime.Today));
        }
    }    // This simple class is used to demonstrate binding to a type.
    public class DemoCustomer
    {        // These fields hold the data that backs the public properties.
        private DateTime firstOrderDateValue;        private Guid idValue;        private string custNameValue;        public string CustomerName
        {            get { return custNameValue; }            set { custNameValue = value; }
        }        // This is a property that represents a birth date.
        public DateTime FirstOrder
        {            get
            {                return this.firstOrderDateValue;
            }            set
            {                if (value != this.firstOrderDateValue)
                {                    this.firstOrderDateValue = value;
                }
            }
        }        // This is a property that represents a customer ID.
        public Guid ID
        {            get
            {                return this.idValue;
            }
        }        public DemoCustomer()
        {
            idValue = Guid.NewGuid();
        }        public DemoCustomer(DateTime FirstOrderDate)
        {
            FirstOrder = FirstOrderDate;
            idValue = Guid.NewGuid();
        }
    }
}
相關文章
相關標籤/搜索