WinForm之BindingSource基础操作实例教程
通常我们在进行数据绑定的时候,常用的数据源有DataSet、DataTable、BindingList<T>、还有强类型数据源。今天我们来通过实例了解一下BindingSource组建,分享给大家供大家参考借鉴之用。
BindingSource的两个用途:
(1)首先,它提供一个将窗体上的控件绑定到数据的间接层。这是通过将BindingSource组件绑定到数据源,然后将窗体上的控件绑定到BindingSource组件来完成的。与数据的所有进一步交互(包括导航、排序、筛选和更新)都是通过调用BindingSource组件来完成的。
(2)其次,BindingSource组件可以充当强类型数据源。使用Add方法向BindingSource组件添加类型会创建一个该类型的列表。
一、对BindingSource的基础操作——增删改查
实例代码如下:
publicpartialclassForm1:Form
{
//注当前DGV已经绑定到ID和Name列
privateBindingSourcesource=newBindingSource();
publicForm1()
{
InitializeComponent();
}
//窗体加载
privatevoidForm1_Load(objectsender,EventArgse)
{
this.source.DataSource=typeof(Custom);
this.dataGridView1.DataSource=this.source;
}
//添加
privatevoidbutton1_Click(objectsender,EventArgse)
{
this.source.Add(newCustom(1,"A"));
this.source.Add(newCustom(2,"B"));
}
//删除
privatevoidbutton2_Click(objectsender,EventArgse)
{
this.source.RemoveAt(0);
}
//排序【有问题】
privatevoidbutton3_Click(objectsender,EventArgse)
{
this.source.Sort="IDASC";
this.source.ResetBindings(false);
}
//筛选【有问题】
privatevoidbutton4_Click(objectsender,EventArgse)
{
this.source.Filter="ID=1";
this.source.ResetBindings(false);
}
//向下移动
privatevoidbutton5_Click(objectsender,EventArgse)
{
this.source.MoveNext();
MessageBox.Show(this.source.Position.ToString());
}
//向上移动
privatevoidbutton9_Click(objectsender,EventArgse)
{
this.source.MovePrevious();
MessageBox.Show(this.source.Position.ToString());
}
//获取当前项
privatevoidbutton6_Click(objectsender,EventArgse)
{
Customcustom=(Custom)this.source.Current;
MessageBox.Show("所处的位置:"+this.source.IndexOf(custom).ToString());
MessageBox.Show("custom.Name:"+custom.Name);
}
//修改当前项
privatevoidbutton7_Click(objectsender,EventArgse)
{
Customcustom=(Custom)this.source.Current;
custom.Name="修改后的值";
this.source.ResetCurrentItem();
}
//删除当前项
privatevoidbutton8_Click(objectsender,EventArgse)
{
Customcustom=(Custom)this.source.Current;
this.source.Remove(custom);
}
}
//自定义类字段必须属性公开化
publicclassCustom
{
publicCustom()
{}
publicCustom(intID,stringName)
{
this.ID=ID;
this.Name=Name;
}
privateintid;
publicintID
{
get{returnid;}
set{id=value;}
}
privatestringname;
publicstringName
{
get{returnname;}
set{name=value;}
}
}
二、 下面的示例演示如何在两种不同情况下绑定DBNull值。
第一种情况演示如何设置字符串属性的NullValue;第二种情况演示如何设置图像属性的NullValue。
下面的示例演示如何在两种不同情况下绑定DBNull值。第一种情况演示如何设置字符串属性的NullValue;第二种情况演示如何设置图像属性的NullValue。
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Data.SqlClient;
usingSystem.Windows.Forms;
namespaceDBNullCS
{
publicclassForm1:Form
{
publicForm1()
{
this.Load+=newEventHandler(Form1_Load);
}
//Thecontrolsandcomponentsweneedfortheform.
privateButtonbutton1;
privatePictureBoxpictureBox1;
privateBindingSourcebindingSource1;
privateTextBoxtextBox1;
privateTextBoxtextBox2;
//Datatabletoholdthedatabasedata.
DataTableemployeeTable=newDataTable();
voidForm1_Load(objectsender,EventArgse)
{
//Basicformsetup.
this.pictureBox1=newPictureBox();
this.bindingSource1=newBindingSource();
this.textBox1=newTextBox();
this.textBox2=newTextBox();
this.button1=newButton();
this.pictureBox1.Location=newSystem.Drawing.Point(20,20);
this.pictureBox1.Size=newSystem.Drawing.Size(174,179);
this.textBox1.Location=newSystem.Drawing.Point(25,215);
this.textBox1.ReadOnly=true;
this.textBox2.Location=newSystem.Drawing.Point(25,241);
this.textBox2.ReadOnly=true;
this.button1.Location=newSystem.Drawing.Point(200,103);
this.button1.Text="MoveNext";
this.button1.Click+=newSystem.EventHandler(this.button1_Click);
this.ClientSize=newSystem.Drawing.Size(292,273);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.pictureBox1);
this.ResumeLayout(false);
this.PerformLayout();
//Createtheconnectionstringandpopulatethedatatable
//withdata.
stringconnectionString="IntegratedSecurity=SSPI;"+
"PersistSecurityInfo=False;InitialCatalog=Northwind;"+
"DataSource=localhost";
SqlConnectionconnection=newSqlConnection();
connection.ConnectionString=connectionString;
SqlDataAdapteremployeeAdapter=
newSqlDataAdapter(newSqlCommand("Select*fromEmployees",connection));
connection.Open();
employeeAdapter.Fill(employeeTable);
//SettheDataSourcepropertyoftheBindingSourcetotheemployeetable.
bindingSource1.DataSource=employeeTable;
//SetupthebindingtotheReportsTocolumn.
BindingreportsToBinding=textBox2.DataBindings.Add("Text",bindingSource1,
"ReportsTo",true);
//SettheNullValuepropertyforthisbinding.
reportsToBinding.NullValue="NoManager";
//SetupthebindingforthePictureBoxusingtheAddmethod,setting
//thenullvalueinmethodcall.
pictureBox1.DataBindings.Add("Image",bindingSource1,"Photo",true,
DataSourceUpdateMode.Never,newBitmap(typeof(Button),"Button.bmp"));
//Setuptheremainingbinding.
textBox1.DataBindings.Add("Text",bindingSource1,"LastName",true);
}
//Movethroughthedatawhenthebuttonisclicked.
privatevoidbutton1_Click(objectsender,EventArgse)
{
bindingSource1.MoveNext();
}
[STAThread]
staticvoidMain()
{
Application.EnableVisualStyles();
Application.Run(newForm1());
}
}
}
希望本文实例对大家C#程序设计的学习有所帮助!