Displaying data records with ListView in Visual C #

Source: Internet
Author: User

If you want to display data records from a database in your program, the first display tool you want to use must be a DataGrid. Of course, using a DataGrid to display data logging is a common and simple method. But in terms of program control, it is not so arbitrary. This article is to introduce another way of displaying data record--using ListView to display data records, because he is manually added to the record, although in the design of a little cumbersome, but for those in the special display requirements, but often can meet the requirements.

With many components defined in the. Net FrameWork SDK, Visual C # is enriching its own interface by acquiring instances of these components. The list (ListView) is a commonly used component in the program design, because of its own characteristic, often be used to display the relatively huge data information. This article is to use his characteristics to see how it can display data records.

A Design and operation of the environment

(1). Microsoft Windows 2000 Professional Edition

(2). Net FrameWork SDK Beta 2

(3). Microsoft Data acess Component 2.6 (MDAC2.6)

Two The concrete idea of program design

(1). First, set up a data connection, open the dataset

(2). Initialize the list and make the display condition of the list conform to the condition of the data record

(3). Traversing data records in a dataset, adding records to the list in the traversal

(4). Close data set, close data connection

Three Specific steps to achieve

(1). First, set up a data connection, open the dataset

For how to establish the data connection and obtain the content of the dataset can refer to an article of this site-"in Visual C # access to different databases," in this article on this issue has a more detailed introduction, this article does not describe, the specific implementation statements are as follows:

// 定义数据连接的字符串,程序中使用的是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). Initializes the list and makes the display condition of the list conform to the conditions of the data record. What needs to be explained is that in the source code below, LV is an instance of a ListView defined in class

// 初始化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 ;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.