c# listview 使用方法
来源:互联网 发布:期货库存数据 编辑:程序博客网 时间:2024/05/21 20:26
这里是其一个比较简单的用法;
其中: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();
}
- c# listview 使用方法
- C# ListView控件的使用方法 简介
- C# ListView控件的使用方法 简介
- C#中listview的使用方法介绍
- ListView使用方法
- listview使用方法
- listview 使用方法
- ListView的使用方法
- GridView、ListView、Repeater使用方法
- LISTVIEW的使用方法
- android ListView的使用方法
- ListView控件的使用方法
- ListView控件使用方法
- ListView的基本使用方法
- Android ListView使用方法
- ListView的使用方法
- android Listview使用方法
- 【转载】ListView的使用方法
- 各种字符串类型之间的转换
- jQuery 的一些不当的运用
- google+的+1动画
- 关于AndroidManifest.xm
- C++ builder 6 编译时提示Unable to open file 'DCLUSR.LIB' 之解决办法
- c# listview 使用方法
- 性格色彩测试android程序开发之四--响应dialog事件的处理
- java treevieww 根据mouseevent坐标获得item
- Microsoft OutLook 部分邮件不能收到的经验。
- java 拖拽文件 和windows交互
- CStatic贴图
- ArcEngine中getLayer方法的UID参数的详解
- 【并查集】食物链
- android颜色对应的xml配置值