c# listview 使用方法

来源:互联网 发布:期货库存数据 编辑:程序博客网 时间:2024/05/21 20:26
c#的ListView是Windows应用程序中经常用到;
这里是其一个比较简单的用法;
其中:ListView为:eListView;
用法如下所示:

//程序初始设置其基本属性,注释如下,并用loadData()得到其显示内容;
//设置eListView的基本属性
//loadData()函数得到表项,并显示
private void EmailForm_Load(object sender, System.EventArgs e)
{
eListView.GridLines = true ;//显示各个记录的分隔线 
eListView.FullRowSelect = true ;//要选择就是一行 
eListView.View = View.Details ;//定义列表显示的方式 
eListView.Scrollable = true ;//需要时候显示滚动条 
eListView.MultiSelect = false ; // 不可以多行选择 
eListView.HeaderStyle = ColumnHeaderStyle.Clickable; 

loadData();
}

//***********************得到数据集并绑定到rListView控件************/
//清空eListView
//设置表头。
//执行数据库查询操作,得到表中所要显示的数据
//数据按行绑定到eListView
/*********************************************************************/
private void loadData()
{
this.eListView.Clear();

// 针对数据库的字段名称,建立与之适应显示表头 
eListView.Columns.Add ( "序号" , 50, HorizontalAlignment.Center ) ;
eListView.Columns.Add ( "用户名" , 60 , HorizontalAlignment.Center ) ; 
eListView.Columns.Add ( "邮件地址" , 150 , HorizontalAlignment.Center ) ; 
eListView.Visible = true ; 


string strSql = string.Format("select name,mailbox from mail ");
DBConnection dbcon = new DBConnection();
IDataReader read = dbcon.getRead(strSql);
int i =0;
while(read.Read())
{
i ++;
ListViewItem Item = new ListViewItem ( ) ; 
Item.SubItems.Clear ( ) ; 
Item.SubItems[0].Text = i.ToString() ;
Item.SubItems.Add ( read.GetString(0)) ; 
Item.SubItems.Add ( read.GetString(1)) ;
eListView.Items.Add ( Item ) ;
}
dbcon.close();//关闭数据库链接 
}

通过以上就可以得到相应的数据,并显示到ListView中;

以下函数是对ListView双击取数据的一个例子:

//依据事件索引和对应的列数,把相关的内容对应的显示在相关TextBox控件中
private void eListView_SelectedIndexChanged(object sender, System.EventArgs e)
{
if(this.eListView.SelectedItems.Count >0)
{
this.tbName.Text = eListView.SelectedItems[0].SubItems[1].Text;
this.tbEmail.Text = eListView.SelectedItems[0].SubItems[2].Text;
}

}



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace _8_07
{
public partial class frmListView : Form
{
public frmListView()
{
InitializeComponent();
}
private void frmListView_Load(object sender, EventArgs e)
{
}
private void bntDelete_Click(object sender, EventArgs e)
{
lvStudent.Items.Clear();
}
private void bntAdd_Click(object sender, EventArgs e)
{
this.lvStudent.View = View.Details;
this.lvStudent.FullRowSelect = true;
SqlConnection con = new SqlConnection("server=(local);uid=sa;pwd=;database=zhy");
con.Open();
SqlCommand com = new SqlCommand("select * from student", con);
SqlDataReader dr = com.ExecuteReader();
this.lvStudent.Items.Clear();
while (dr.Read())
{
ListViewItem lt = new ListViewItem(dr.GetValue(0).ToString());
lt.SubItems.Add(dr.GetValue(1).ToString());
lt.SubItems.Add(dr.GetValue(2).ToString());
this.lvStudent.Items.Add(lt);
}
dr.Close();
con.Close();
this.lvStudent.Alignment = ListViewAlignment.SnapToGrid;
this.lvStudent.GridLines = true;
}
private void bntEsce_Click(object sender, EventArgs e)
{
Application.Exit();
}