使用BindingSource组件将Windows Forms控件绑定到类型

Posted 清语堂

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用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();
        }
    }
}

 

以上是关于使用BindingSource组件将Windows Forms控件绑定到类型的主要内容,如果未能解决你的问题,请参考以下文章

使用两个 Windows 窗体 DataGridView 控件创建一个主/从窗体

如何:用BindingSource将Windows Forms控件绑定到工厂对象

用BindingSource将Windows窗体控件绑定到DBNull数据库值

c# BindingSource 类

WinForm之BindingSource基础操作实例教程

使用实体框架过滤BindingSource