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

推荐订阅源

S
Secure Thoughts
Security Latest
Security Latest
Simon Willison's Weblog
Simon Willison's Weblog
O
OpenAI News
GbyAI
GbyAI
L
LINUX DO - 最新话题
A
Arctic Wolf
T
Tor Project blog
G
GRAHAM CLULEY
I
InfoQ
博客园_首页
IT之家
IT之家
The Register - Security
The Register - Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
The GitHub Blog
The GitHub Blog
Blog — PlanetScale
Blog — PlanetScale
N
Netflix TechBlog - Medium
K
Kaspersky official blog
博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
U
Unit 42
PCI Perspectives
PCI Perspectives
量子位
P
Palo Alto Networks Blog
S
Securelist
T
Troy Hunt's Blog
博客园 - 【当耐特】
Recorded Future
Recorded Future
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Security Affairs
Engineering at Meta
Engineering at Meta
T
The Blog of Author Tim Ferriss
博客园 - 聂微东
罗磊的独立博客
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
B
Blog RSS Feed
NISL@THU
NISL@THU
C
Cisco Blogs
T
Threatpost
有赞技术团队
有赞技术团队
Forbes - Security
Forbes - Security
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
T
The Exploit Database - CXSecurity.com
Cloudbric
Cloudbric
Cyberwarzone
Cyberwarzone
Google DeepMind News
Google DeepMind News
C
Cyber Attacks, Cyber Crime and Cyber Security

博客园 - 什么都不知道

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属性用来放数据表加字段,封装更新的内容。(代码懒得写了)