1. 程式人生 > >在Visual C#中用ListView顯示資料記錄

在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 ( ) ; //
關閉資料連線

四. 程式執行結果介面和程式原始碼(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,然後在其中建立一張資料表,欄位如下:nameHomePhoneWorkPhoneMobilePhoneCityAddressEmail。此時執行編譯後的程式就可以得到如下執行介面:

         圖01:用ListView顯示資料記錄

  五.對於其他資料庫如何處理  對於其他資料庫也需要用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元件的具體使用有了具體的和更高的認識。

listView1的【view】屬性設定成【details】,然後設定【columns】屬性,新增4列,然後:

listView1.Items.Clear();

for (int i = 0; i < 10; i++)

{

ListViewItem lVI = new ListViewItem("ddddddddddd");

lVI.SubItems.Add("kkkkkk");

lVI.SubItems.Add("lllll");

lVI.SubItems.Add("oooooooo");

listView1.Items.Add(lVI);

}

hegaobao :程式碼是新增行的,列在設計裡新增好的,