标签:控件 Windows System Forms button1 new using public BindingSource
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(); } } }
标签:控件,Windows,System,Forms,button1,new,using,public,BindingSource 来源: https://www.cnblogs.com/wfy680/p/14798075.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。