C#使用DataBindings属性绑定不同类型的数据源
C#使用DataBindings属性绑定不同类型的数据源
C# Code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//DataTable数据源
DataTable dt = this.CreateData();
textBox1.DataBindings.Add("Text", dt, "Code");
textBox2.DataBindings.Add("Text", dt, "Name");
textBox3.DataBindings.Add("Text", dt, "Price");
//实例化对象
MyData data = new MyData();
data.Code = "CSFramework";
data.Name = "C/S系统开发框架";
data.Price = (decimal)999.99;
textBox4.DataBindings.Add("Text", data, "Code");
textBox5.DataBindings.Add("Text", data, "Name");
textBox6.DataBindings.Add("Text", data, "Price");
}
/// <summary>
/// 对象实体类
/// </summary>
class MyData
{
private string _Code;
private string _Name;
private decimal _Price;
public decimal Price
{
get { return _Price; }
set { _Price = value; }
}
public string Code
{
get { return _Code; }
set { _Code = value; }
}
public string Name
{
get { return _Name; }
set { _Name = value; }
}
}
/// <summary>
/// DataTable数据源
/// </summary>
/// <returns></returns>
private DataTable CreateData()
{
DataTable dt = new DataTable();
dt.Columns.Add("Code", typeof(string));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Price", typeof(decimal)); //数字
dt.Rows.Add(new object[] { "CSFramework", "C/S系统开发框架", 999.99 });
dt.AcceptChanges();
return dt;
}
}
//来源:C/S框架网(www.csframework.com) QQ:1980854898
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//DataTable数据源
DataTable dt = this.CreateData();
textBox1.DataBindings.Add("Text", dt, "Code");
textBox2.DataBindings.Add("Text", dt, "Name");
textBox3.DataBindings.Add("Text", dt, "Price");
//实例化对象
MyData data = new MyData();
data.Code = "CSFramework";
data.Name = "C/S系统开发框架";
data.Price = (decimal)999.99;
textBox4.DataBindings.Add("Text", data, "Code");
textBox5.DataBindings.Add("Text", data, "Name");
textBox6.DataBindings.Add("Text", data, "Price");
}
/// <summary>
/// 对象实体类
/// </summary>
class MyData
{
private string _Code;
private string _Name;
private decimal _Price;
public decimal Price
{
get { return _Price; }
set { _Price = value; }
}
public string Code
{
get { return _Code; }
set { _Code = value; }
}
public string Name
{
get { return _Name; }
set { _Name = value; }
}
}
/// <summary>
/// DataTable数据源
/// </summary>
/// <returns></returns>
private DataTable CreateData()
{
DataTable dt = new DataTable();
dt.Columns.Add("Code", typeof(string));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Price", typeof(decimal)); //数字
dt.Rows.Add(new object[] { "CSFramework", "C/S系统开发框架", 999.99 });
dt.AcceptChanges();
return dt;
}
}
//来源:C/S框架网(www.csframework.com) QQ:1980854898
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网