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

推荐订阅源

Help Net Security
Help Net Security
S
SegmentFault 最新的问题
T
The Blog of Author Tim Ferriss
J
Java Code Geeks
博客园 - 三生石上(FineUI控件)
月光博客
月光博客
C
Check Point Blog
M
MIT News - Artificial intelligence
GbyAI
GbyAI
H
Hackread – Cybersecurity News, Data Breaches, AI and More
U
Unit 42
D
Docker
G
Google Developers Blog
云风的 BLOG
云风的 BLOG
H
Help Net Security
D
DataBreaches.Net
Microsoft Azure Blog
Microsoft Azure Blog
B
Blog RSS Feed
Engineering at Meta
Engineering at Meta
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
B
Blog
Cloudbric
Cloudbric
Blog — PlanetScale
Blog — PlanetScale
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
T
Troy Hunt's Blog
N
News | PayPal Newsroom
V2EX - 技术
V2EX - 技术
H
Heimdal Security Blog
S
Security @ Cisco Blogs
V
Visual Studio Blog
The Last Watchdog
The Last Watchdog
博客园 - Franky
大猫的无限游戏
大猫的无限游戏
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Webroot Blog
Webroot Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
C
Cyber Attacks, Cyber Crime and Cyber Security
Last Week in AI
Last Week in AI
爱范儿
爱范儿
博客园 - 聂微东
S
Securelist
小众软件
小众软件
酷 壳 – CoolShell
酷 壳 – CoolShell
Cisco Talos Blog
Cisco Talos Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
C
CXSECURITY Database RSS Feed - CXSecurity.com
V
Vulnerabilities – Threatpost
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
O
OpenAI News
Apple Machine Learning Research
Apple Machine Learning Research

博客园 - 什么都不知道

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