| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3244 人关注过本帖, 1 人收藏
标题:[转帖]在Visual C#中用ListView显示数据记录
只看楼主 加入收藏
yushengou
Rank: 1
等 级:新手上路
帖 子:401
专家分:0
注 册:2005-3-30
收藏(1)
 问题点数:0 回复次数:23 
[转帖]在Visual C#中用ListView显示数据记录
如果要你在程序中显示数据库中的数据记录,你首先想用的显示工具肯定是DataGrid。当然用DataGrid显示数据记录是一种既常用又简单的方法。但是在程序控制方面,它却无法那么随心所欲。本文就是介绍另外一种显示数据记录的方法--用ListView来显示数据记录,由于他是手动加入记录,虽然在程序设计中稍微烦琐了些,但对于那些在特殊的显示要求,却往往能够满足要求。 在.Net FrameWork SDK中定义了许多组件,Visual C#就是通过获得这些组件的实例来丰富自己的界面的。列表(ListView)是程序设计中一个常用的组件,由于其自身的特点,往往被使用显示比较庞大的数据信息。本文就是利用他的这个特点来看看它如何来显示数据记录。 一. 程序设计和运行的环境   (1).微软视窗2000专业版本   (2)..Net FrameWork SDK Beta 2   (3).Microsoft Data Acess Component 2.6 (MDAC2.6) 二. 程序设计的具体思路   (1).首先要建立数据连接,打开数据集   (2).对列表进行初始化,并使得列表的显示条件符合数据记录的条件   (3).对数据集中的数据记录进行遍历,在遍历中添加记录到列表中   (4).关闭数据集,关闭数据连接 三. 具体的实现步骤   (1).首先要建立数据连接,打开数据集   对于如何建立数据连接和获得数据集的内容可以参考本站的一篇文章--《在Visual C#中访问不同的数据库》,在此文中对此类问题有比较详细的介绍,本文就不多叙述,具体实现语句如下:
// 定义数据连接的字符串,程序中使用的是Acess 2000数据库 private static string strConnect = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = " + Application.StartupPath + "\\MY.MDB" ; private OleDbConnection conConnection = new OleDbConnection ( strConnect ) ; OleDbDataReader reader ; // 获得Person里面的所以数据记录 string strCommand = "SELECT * FROM Persons" ; this.conConnection.Open ( ) ; // 打开数据连接 OleDbCommand cmd = new OleDbCommand ( strCommand , conConnection ) ; reader = cmd.ExecuteReader ( ) ; file://获得数据集
     (2).对列表进行初始化,并使得列表的显示条件符合数据记录的条件。需要说明的是在下面源代码中,lv是在Class中定义的一个ListView的一个实例
// 初始化ListView lv = new ListView ( ) ; lv.Left = 0 ; lv.Top = 0 ; lv.Width = 700 ; lv.Height = this.ClientRectangle.Height ; lv.GridLines = true ; file://显示各个记录的分隔线 lv.FullRowSelect = true ; file://要选择就是一行 lv.View = View.Details ; file://定义列表显示的方式 lv.Scrollable = true ; file://需要时候显示滚动条 lv.MultiSelect = false ; // 不可以多行选择 lv.HeaderStyle = ColumnHeaderStyle.Nonclickable ; // 针对数据库的字段名称,建立与之适应显示表头 lv.Columns.Add ( "姓名" , 60 , HorizontalAlignment.Right ) ; lv.Columns.Add ( "住宅电话" , 100 , HorizontalAlignment.Left ) ; lv.Columns.Add ( "办公电话" , 100 , HorizontalAlignment.Left ) ; lv.Columns.Add ( "移动电话" , 100 , HorizontalAlignment.Left ) ; lv.Columns.Add ( "居住地点" , 100 , HorizontalAlignment.Left ) ; lv.Columns.Add ( "工作单位" , 100 , HorizontalAlignment.Left ) ; lv.Columns.Add ( "电子邮件" , 100 , HorizontalAlignment.Left ) ; lv.Visible = true ;
  (3).对数据集中的数据记录进行遍历,在遍历中添加记录到列表中。   可以利用数据集中的Read ( )方法,来实现对数据记录的遍历,Read ( )方法是首先指向首数据记录,并判断从此记录是否为尾记录,如果不是则返回false,如果是则返回true。并且如果不是尾记录则自动把数据指针移到下一条记录上,然后在判断此记录是否是尾记录,如此循环,直至到尾记录为止。根据此可设计以下代码:
while ( reader.Read ( ) ) { ListViewItem li = new ListViewItem ( ) ; li.SubItems.Clear ( ) ; li.SubItems[0].Text = reader["name"].ToString ( ) ; li.SubItems.Add ( reader["HomePhone"].ToString ( ) ) ; li.SubItems.Add ( reader["WorkPhone"].ToString ( ) ) ; li.SubItems.Add ( reader["MobilePhone"].ToString ( ) ) ; li.SubItems.Add ( reader["City"].ToString ( ) ) ; li.SubItems.Add ( reader["Address"].ToString ( ) ) ; li.SubItems.Add ( reader["Email"].ToString ( ) ) ; lv.Items.Add ( li ) ; }
  (4). 关闭数据集,关闭数据连接。 关闭数据集和关闭数据连接是很容易的,只要调用这二个对象的Close()方法即可,也只要调用在程序中具体如下:
reader.Close ( ) ; file://关闭数据集 this.conConnection.Close ( ) ; // 关闭数据连接
搜索更多相关主题的帖子: 数据记录 ListView Visual 数据库 转帖 
2005-05-13 10:00
yushengou
Rank: 1
等 级:新手上路
帖 子:401
专家分:0
注 册:2005-3-30
收藏
得分:0 
四. 程序运行结果界面和程序源代码(list.cs):   程序源代码:
using System ; using System.Windows.Forms ; using System.Drawing ; using System.Data ; using System.Data.OleDb ; class MainForm : Form { // 定义数据连接的字符串 private static string strConnect = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = " + Application.StartupPath + "\\MY.MDB" ; private OleDbConnection conConnection = new OleDbConnection ( strConnect ) ; private ListView lv ; public MainForm ( ) { // 初始化Form this.Left = 0 ; this.Top = 0 ; this.Text = "在ListView中显示数据库内容!" ; // 初始化ListView lv = new ListView ( ) ; lv.Left = 0 ; lv.Top = 0 ; lv.Width = 700 ; lv.Height = this.ClientRectangle.Height ; lv.GridLines = true ; file://显示各个记录的分隔线 lv.FullRowSelect = true ; file://要选择就是一行 lv.View = View.Details ; file://定义列表显示的方式 lv.Scrollable = true ; file://需要时候显示滚动条 lv.MultiSelect = false ; // 不可以多行选择 lv.HeaderStyle = ColumnHeaderStyle.Nonclickable ; // 针对数据库的字段名称,建立与之适应显示表头 lv.Columns.Add ( "姓名" , 60 , HorizontalAlignment.Right ) ; lv.Columns.Add ( "住宅电话" , 100 , HorizontalAlignment.Left ) ; lv.Columns.Add ( "办公电话" , 100 , HorizontalAlignment.Left ) ; lv.Columns.Add ( "移动电话" , 100 , HorizontalAlignment.Left ) ; lv.Columns.Add ( "居住地点" , 100 , HorizontalAlignment.Left ) ; lv.Columns.Add ( "工作单位" , 100 , HorizontalAlignment.Left ) ; lv.Columns.Add ( "电子邮件" , 100 , HorizontalAlignment.Left ) ; lv.Visible = true ; OleDbDataReader reader ; string strCommand = "SELECT * FROM Persons" ; this.conConnection.Open ( ) ;// 打开数据连接 OleDbCommand cmd = new OleDbCommand ( strCommand , conConnection ) ; reader = cmd.ExecuteReader ( ) ;//获得数据集 // 不断往列表中添加数据记录 while ( reader.Read ( ) ) { ListViewItem li = new ListViewItem ( ) ; li.SubItems.Clear ( ) ; li.SubItems[0].Text = reader["name"].ToString ( ) ; li.SubItems.Add ( reader["HomePhone"].ToString ( ) ) ; li.SubItems.Add ( reader["WorkPhone"].ToString ( ) ) ; li.SubItems.Add ( reader["MobilePhone"].ToString ( ) ) ; li.SubItems.Add ( reader["City"].ToString ( ) ) ; li.SubItems.Add ( reader["Address"].ToString ( ) ) ; li.SubItems.Add ( reader["Email"].ToString ( ) ) ; lv.Items.Add ( li ) ; } reader.Close ( ) ; // 关闭数据集 // 在Form中添加此列表 this.Controls.Add ( lv ) ; // 关闭Form的时候,同时也关闭数据连接 this.Closed+=new EventHandler ( this_Closed ) ; } protected void this_Closed ( object sender , EventArgs eArgs ) { this.conConnection.Close ( ) ; file://关闭数据连接 } public static void Main ( ) { Application.Run ( new MainForm ( ) ) ; } }
  在成功编译了上面源程序代码以后,在同一目录下建立一个Acess 2000的数据库,命名为MY.MDB,然后在其中建立一张数据表,字段如下:name,HomePhone,WorkPhone,MobilePhone,City,Address,Email。此时运行编译后的程序就可以得到如下运行界面: [IMG]http://www.yesky.com/20011102/jt-2001-11-2-image002.jpg[/IMG] 五. 对于其他数据库如何处理   对于其他数据库也需要用ListView来显示数据记录,和上面的一个主要区别在于建立不同的数据字符串,下面就以SQL Server 7.0为例来简要说明:   如果访问的数据库是SQL Server 7.0,只需要把上面源代码中的一条语句:
private static string strConnect = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = " + Application.StartupPath + "\\MY.MDB" ;
  改变成:
private static string strConnect = "Provider=SQLOLEDB.1 ; Persist Security Info=False ; User ID = sa ; Initial Catalog=数据库名称; Data Source = 服务器名称 " ;
  即可。   六. 总结   本文试图用另外一种方法来显示数据记录,虽然在使用的时候比起正常的方法要烦琐 些,但有更高的灵活度,并且也使得我们对于ListView组件的具体使用有了具体的和更高的认识。

我是初学者,希望大家能多多帮助我 /bbs/showimg.asp?BoardID=34&filename=2005-4/200542294030151.gif" border="0" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('http://bbs./bbs/showimg.asp?BoardID=34&filename=2005-4/200542294030151.gif');}" onmousewheel="return imgzoom(this);" alt="" />
2005-05-13 10:01
eastsnake
Rank: 1
等 级:新手上路
帖 子:127
专家分:0
注 册:2005-3-8
收藏
得分:0 
试了一下,相当不错

程序员是男孩,语言是女孩; 每个男孩都希望能交往更多的女孩; 但是却没有一个男孩真正了解一个女孩; 男孩总是不能专心一个女孩,而女孩却总是在变~
2005-05-13 10:59
yushengou
Rank: 1
等 级:新手上路
帖 子:401
专家分:0
注 册:2005-3-30
收藏
得分:0 
谢谢回贴。
都没人看。

我是初学者,希望大家能多多帮助我 /bbs/showimg.asp?BoardID=34&filename=2005-4/200542294030151.gif" border="0" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('http://bbs./bbs/showimg.asp?BoardID=34&filename=2005-4/200542294030151.gif');}" onmousewheel="return imgzoom(this);" alt="" />
2005-05-13 11:08
live41
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:12442
专家分:0
注 册:2004-7-22
收藏
得分:0 
我看,很快有人看.
2005-05-13 13:04
yushengou
Rank: 1
等 级:新手上路
帖 子:401
专家分:0
注 册:2005-3-30
收藏
得分:0 
处男转。
没经验滴说

我是初学者,希望大家能多多帮助我 /bbs/showimg.asp?BoardID=34&filename=2005-4/200542294030151.gif" border="0" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('http://bbs./bbs/showimg.asp?BoardID=34&filename=2005-4/200542294030151.gif');}" onmousewheel="return imgzoom(this);" alt="" />
2005-05-13 13:37
幻风幻云
Rank: 1
等 级:新手上路
帖 子:762
专家分:0
注 册:2005-1-14
收藏
得分:0 
很好阿

2005-05-14 00:47
yushengou
Rank: 1
等 级:新手上路
帖 子:401
专家分:0
注 册:2005-3-30
收藏
得分:0 
看的人是有啦。
就是不回贴啊。

我是初学者,希望大家能多多帮助我 /bbs/showimg.asp?BoardID=34&filename=2005-4/200542294030151.gif" border="0" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('http://bbs./bbs/showimg.asp?BoardID=34&filename=2005-4/200542294030151.gif');}" onmousewheel="return imgzoom(this);" alt="" />
2005-05-14 09:04
yichen
Rank: 1
等 级:新手上路
帖 子:303
专家分:0
注 册:2005-3-9
收藏
得分:0 
要试一下,看看!

衣带渐宽终不悔, 为伊消得人憔悴。 纸上得来终觉浅, 绝知此事要躬行。
2005-05-14 19:47
幻风幻云
Rank: 1
等 级:新手上路
帖 子:762
专家分:0
注 册:2005-1-14
收藏
得分:0 
我的2003系统又崩溃了

好命苦阿

刚装的.net。

2005-05-15 00:18
快速回复:[转帖]在Visual C#中用ListView显示数据记录
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025475 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved