使用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.8

例子

下面的代码通过使用BindingSource组件将DataGridView控件绑定到自定义类型。在运行示例时,您会注意到在用数据填充控件之前,DataGridView的标签列反映了对象的属性。该示例具有一个Add Customer按钮,用于将数据添加到DataGridView控件。当您单击按钮时,新对象将添加到BindingSource中。在现实世界中,数据可能是通过调用Web服务或其他数据源获得的。

BindingSource绑定到一个类型,然后可以对这个BindingSource添加、删除数据

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();
        }
    }
}

 

上一篇:Wpf TextChanged事件导致死循环,事件触发循环问题


下一篇:一句 Task.Result 就死锁, 这代码还怎么写?