在构建与数据交互的控件时,有时会发现有必要将控件绑定到生成其他对象的对象或方法。这样的对象或方法称为工厂。例如,您的数据源可能是方法调用的返回值,而不是内存或类型中的对象。您可以将控件绑定到这种数据源,只要该源返回一个集合即可。
您可以使用BindingSource控件轻松地将控件绑定到工厂对象。
例子
下面的示例演示如何通过使用BindingSource控件将DataGridView控件绑定到工厂方法。工厂方法名为 GetOrdersByCustomerId,它将在Northwind数据库中返回给定客户的所有订单。
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication2 { static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } }
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 { // This is the TextBox for entering CustomerID values. private TextBox customerIdTextBox = new TextBox(); // This is the DataGridView that displays orders for the // specified customer. private DataGridView customersDataGridView = new DataGridView(); // This is the BindingSource for binding the database query // result set to the DataGridView. private BindingSource ordersBindingSource = new BindingSource(); public Form1() { // Set up the CustomerID TextBox. this.customerIdTextBox.Location = new Point(100, 200); this.customerIdTextBox.Size = new Size(500, 30); this.customerIdTextBox.Text = "Enter a valid Northwind CustomerID, for example: ALFKI," + " then RETURN or click outside the TextBox"; this.customerIdTextBox.Leave += new EventHandler(customerIdTextBox_Leave); this.customerIdTextBox.KeyDown += new KeyEventHandler(customerIdTextBox_KeyDown); this.Controls.Add(this.customerIdTextBox); // Set up the DataGridView. customersDataGridView.Dock = DockStyle.Top; this.Controls.Add(customersDataGridView); // Set up the form. this.Size = new Size(800, 500); this.Load += new EventHandler(Form1_Load); } // This event handler binds the BindingSource to the DataGridView // control's DataSource property. private void Form1_Load( System.Object sender, System.EventArgs e) { // Attach the BindingSource to the DataGridView. this.customersDataGridView.DataSource = this.ordersBindingSource; } // This is a static factory method. It queries the Northwind // database for the orders belonging to the specified // customer and returns an IEnumerable. public static IEnumerable GetOrdersByCustomerId(string id) { // Open a connection to the database. string connectString = "Integrated Security=SSPI;" + "Persist Security Info=False;Initial Catalog=Northwind;" + "Data Source= localhost"; SqlConnection connection = new SqlConnection(); connection.ConnectionString = connectString; connection.Open(); // Execute the query. string queryString = String.Format("Select * From Orders where CustomerID = '{0}'", id); SqlCommand command = new SqlCommand(queryString, connection); SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection); return reader; } // These event handlers are called when the user tabs or clicks // out of the customerIdTextBox or hits the return key. // The database is then queried with the CustomerID // in the customerIdTextBox.Text property. void customerIdTextBox_Leave(object sender, EventArgs e) { // Attach the data source to the BindingSource control. this.ordersBindingSource.DataSource = GetOrdersByCustomerId(this.customerIdTextBox.Text); } void customerIdTextBox_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Return) { // Attach the data source to the BindingSource control. this.ordersBindingSource.DataSource = GetOrdersByCustomerId(this.customerIdTextBox.Text); } } } }