Display data records with listview in Visual C #

Source: Internet
Author: User

If you want to display the data records in the database in the program, you must first use the display tool DataGrid. Of course, using the DataGrid to display data records is a common and simple method. However, in terms of program control, it cannot do as you like. This article introduces another method for displaying data records-using listview to display data records, because it is manually added to records, although it is a little cumbersome in programming, but for those with special display requirements, they are often able to meet the requirements.

Many components are defined in the. NET Framework SDK. Visual C # enriches its interface by obtaining instances of these components. List (listview) is a commonly used component in programming. Due to its own characteristics, it is often used to display a large amount of data information. This article uses this feature to see how it displays data records.

I. Environment for Program Design and Operation

(1) Microsoft Windows 2000 Professional Edition

(2). Net Framework SDK beta 2

(3). Microsoft Data acess component 2.6 (mdac2.6)

II. Specific ideas of Program Design

(1) first, you must establish a data connection to open the dataset.

(2) initialize the list and make the display conditions of the List conform to the data record conditions.

(3) traverse the data records in the dataset and add the records to the list in the traversal.

(4) Close the dataset and close the data connection.

Iii. Specific implementation steps

(1) first, you must establish a data connection to open the dataset.

For details about how to establish a data connection and obtain a dataset, refer to an article on this site-access different databases in Visual C #. in this article, we will give a more detailed introduction to this type of problem. The specific implementation statements are as follows:

// Define the data connection string. The program uses the acess 2000 database.
Private Static string strconnect = "provider = Microsoft. Jet. oledb.4.0; Data Source =" +
Application. startuppath + "\ My. mdb ";
Private oledbconnection conconnection = new oledbconnection (strconnect );
Oledbdatareader reader;
// Obtain the data records in person
String strcommand = "select * from persons ";
This. conconnection. open (); // open the data connection
Oledbcommand cmd = new oledbcommand (strcommand, conconnection );
Reader = cmd. executereader (); file: // obtain the dataset

(2) initialize the list and make the display conditions of the List conform to the data record conditions. In the following source code, LV is an instance of listview defined in class.

// Initialize listview
Lv = new listview ();
LV. Left = 0;
LV. Top = 0;
PV. width = 700;
LV. Height = This. clientrectangle. height;
LV. gridlines = true; file: // display the separation lines of each record
LV. fullrowselect = true; file: // select a row.
LV. view = view. Details; file: // defines how the list is displayed.
LV. scrollable = true; file: // display the scroll bar when necessary
LV. multiselect = false; // you cannot select multiple rows.
LV. headerstyle = columnheaderstyle. nonclickable;
// Create an appropriate display header for the field name of the database
LV. Columns. Add ("name", 60, horizontalalignment. Right );
LV. Columns. Add ("residential phone number", 100, horizontalalignment. Left );
LV. Columns. Add ("office phone number", 100, horizontalalignment. Left );
LV. Columns. Add ("Mobile Phone", 100, horizontalalignment. Left );
LV. Columns. Add ("Place of Residence", 100, horizontalalignment. Left );
LV. Columns. Add ("Work Unit", 100, horizontalalignment. Left );
LV. Columns. Add ("email", 100, horizontalalignment. Left );
LV. Visible = true;

(3) traverse the data records in the dataset and add the records to the list in the traversal.
You can use the read () method in the dataset to traverse data records. The read () method first points to the first data record and determines whether the record is the last record, if not, false is returned. If yes, true is returned. In addition, if it is not the end record, the Data Pointer will be automatically moved to the next record, and then judge whether the record is the end record, so that the loop ends until the end record. The following code can be designed based on this:

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 the dataset and close the data connection.

 

It is easy to close a dataset and a data connection. You only need to call the close () method of the two objects, or call the method in the program as follows:

Reader. Close (); file: // close the dataset
This. conconnection. Close (); // close the data connection

4. program running result interface and program source code (list. CS)

Program source code:

Using system;
Using system. Windows. forms;
Using system. drawing;
Using system. Data;
Using system. Data. oledb;
Class mainform: Form
{// Define the data connection string
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 ()
{
// Initialize form
This. Left = 0;
This. Top = 0;
This. Text = "display the database content in listview! ";
// Initialize listview
Lv = new listview ();
LV. Left = 0;
LV. Top = 0;
PV. width = 700;
LV. Height = This. clientrectangle. height;
LV. gridlines = true; file: // display the separation lines of each record
LV. fullrowselect = true; file: // select a row.
LV. view = view. Details; file: // defines how the list is displayed.
LV. scrollable = true; file: // display the scroll bar when necessary
LV. multiselect = false; // you cannot select multiple rows.
LV. headerstyle = columnheaderstyle. nonclickable;
// Create an appropriate display header for the field name of the database
LV. Columns. Add ("name", 60, horizontalalignment. Right );
LV. Columns. Add ("residential phone number", 100, horizontalalignment. Left );
LV. Columns. Add ("office phone number", 100, horizontalalignment. Left );
LV. Columns. Add ("Mobile Phone", 100, horizontalalignment. Left );
LV. Columns. Add ("Place of Residence", 100, horizontalalignment. Left );
LV. Columns. Add ("Work Unit", 100, horizontalalignment. Left );
LV. Columns. Add ("email", 100, horizontalalignment. Left );
LV. Visible = true;
Oledbdatareader reader;
String strcommand = "select * from persons ";
This. conconnection. open (); // open the data connection
Oledbcommand cmd = new oledbcommand (strcommand, conconnection );
Reader = cmd. executereader (); // obtain the dataset
// Add data records to the list
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 (); // close the dataset
// Add this list to form
This. Controls. Add (LV );
// When the form is closed, the data connection is also closed.
This. Closed + = new eventhandler (this_closed );
}
Protected void this_closed (Object sender, eventargs eargs)
{
This. conconnection. Close (); file: // close the data connection
}
Public static void main ()
{
Application. Run (New mainform ());
}
}

After successfully compiling the source code above, create an acess 2000 database in the same directory and name it my. mdb, and then create a data table with the following fields: name, homephone, workphone, mobilephone, city, address, and email. At this time, the compiled program can get the following running interface:

Figure 01: Use listview to display data records

5. How to deal with other databases

For other databases, you also need to use listview to display data records. A major difference from the above is to create different data strings. The following uses SQL Server 7.0 as an example to briefly describe it:

If the accessed database is SQL Server 7.0, you only need to put a statement in the source code above:

private static string strConnect = "Provider = Microsoft.Jet.OLEDB.4.0 ; 
Data Source = " + Application.StartupPath + "\\MY.MDB" ; 

Changed:

private static string strConnect = "Provider=SQLOLEDB.1 ;
Persist Security info = false; user id = sa; initial catalog = database name;
Data Source = server name ";

You can.
Vi. Summary

This article tries to use another method to display data records. Although it is more cumbersome to use than the normal method, it has higher flexibility, it also gives us a specific and higher understanding of the specific use of the listview component.

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.