作者在 2011-07-03 20:11:59 发布以下内容
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 ( ) ) ;
}
}
选中项的文本值:this.listView1.SelectedItems[0].Text;选中项的子项的文本值
this.listView1.SelectedItems[0].SubItems[0].Text;(SubItems[0]表示选中项的第一个子项,第二个子项就是SubItem[1],第三个就是SubItems[2]....以此类推)
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 ( ) ) ;
}
}