您的位置:首页 > 其它

WinForm下TextBox的数据绑定和更新

2004-10-10 13:45 405 查看
一直以来在Form上的TextBox要显示数据库内容,通常就是直接赋值。看了http://www.15seconds.com/issue/040908.htm上讲的要做对应类,现在来说,ORM没什么做的特别好的,所以这么做还是有些累。http://tech.ccidnet.com/pub/article/c1138_a9879_p1.html里面介绍也只是单向绑定,而且实现的不是很好。

于是就想直接将TextBox绑定到DataSet上。就做了下面的例子,用的是pubs库中authors表:

private void Form2_Load(object sender, System.EventArgs e)
private void button1_Click(object sender, System.EventArgs e)
private void FillUIContent()

this.textBox1.TextChanged +=new EventHandler(textBox1_TextChanged);
this.textBox2.TextChanged +=new EventHandler(textBox2_TextChanged);

然后事件处理里面,加上

private void textBox1_TextChanged(object sender, System.EventArgs e)
private void textBox2_TextChanged(object sender, EventArgs e)

然后好了,就可以同步更新DataSet了。



然后呢,想做的好,干脆派生一个TextBox,加一个DataSource用来放数据集, DataMember属性用来放数据表加字段,封装更新的内容。(代码懒得写了)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: