惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
Stack Overflow Blog
Stack Overflow Blog
人人都是产品经理
人人都是产品经理
The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
Vercel News
Vercel News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Y
Y Combinator Blog
The Cloudflare Blog
Last Week in AI
Last Week in AI
B
Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS Blog
V
Visual Studio Blog
博客园 - 三生石上(FineUI控件)
小众软件
小众软件
Google DeepMind News
Google DeepMind News
D
DataBreaches.Net
博客园 - 司徒正美
B
Blog RSS Feed
Microsoft Azure Blog
Microsoft Azure Blog
罗磊的独立博客
Hugging Face - Blog
Hugging Face - Blog
L
LangChain Blog

博客园 - 什么都不知道

DataSet的Xml序列化问题 VB.NET的一个小问题 Access is denied的问题 - 什么都不知道 存储过程output参数问题 SQL Server的效率? datagrid刷新问题 使用Visio DrawingControl的应用开发(补) 突起效果的Label 使用Radio按钮选择DataGrid行 如何在运行时加载不处于应用程序目录下的assembly 使用VSA给程序加上脚本支持 删除所有Windows组件 在ASP.NET中嵌入wml标记 c#中动态装载dll 处理大型xml文件 RedirectToMobilePage的问题 使用Visio 2003 Drawing Control开发应用(3)(4) 使用Visio 2003 Drawing Control开发应用(2) 使用Visio 2003 Drawing Control开发应用(1)
WinForm下TextBox的数据绑定和更新
什么都不知道 · 2004-10-10 · via 博客园 - 什么都不知道

一直以来在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)
{
 
this.sqlDataAdapter1.Fill(authorsNameSet);
 
this.dataGrid1.DataSource = authorsNameSet.Tables[0].DefaultView;
 
this.FillUIContent();
}

private void button1_Click(object sender, System.EventArgs e)
{
 index
++;
 
if(index > this.authorsNameSet.Tables[0].Rows.Count - 1)
 
{
  index 
= 0;
 }
 
 
this.FillUIContent();
}

private void FillUIContent()
{
 
if(this.textBox1.DataBindings["Text"!= null)
 
{
  
this.textBox1.DataBindings.Remove(this.textBox1.DataBindings["Text"]);
 }

 
this.textBox1.DataBindings.Add("Text", authorsNameSet.Tables[0].Rows[index]["au_fname"], "");

 
if(this.textBox2.DataBindings["Text"!= null)
 
{
  
this.textBox2.DataBindings.Remove(this.textBox2.DataBindings["Text"]);
 }

 
this.textBox2.DataBindings.Add("Text", authorsNameSet.Tables[0].Rows[index]["au_lname"], "");
}



上面的是单向绑定,就是说,也只能显示,不能更新回来。那要更新回来怎么办?我试了用TextChange事件。在Form_Load中间加上:

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

然后事件处理里面,加上

private void textBox1_TextChanged(object sender, System.EventArgs e)
{
 
this.authorsNameSet.Tables[0].Rows[index]["au_fname"= this.textBox1.Text;
 CurrencyManager cm 
= (CurrencyManager)this.dataGrid1.BindingContext[this.dataGrid1.DataSource];
 cm.Refresh();
}

private void textBox2_TextChanged(object sender, EventArgs e)
{
 
this.authorsNameSet.Tables[0].Rows[index]["au_lname"= this.textBox2.Text;
 CurrencyManager cm 
= (CurrencyManager)this.dataGrid1.BindingContext[this.dataGrid1.DataSource];
 cm.Refresh();
}

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

然后呢,想做的好,干脆派生一个TextBox,加一个DataSource用来放数据集, DataMember属性用来放数据表加字段,封装更新的内容。(代码懒得写了)