zoukankan      html  css  js  c++  java
  • 使用ListView控件展示数据------的详细解析

    一.ImageList:存储图像集合

    Images 存储的所有图像
    ImageSize 图像的大小
    ColorDepth 颜色数
    TransparentColor 被视为透明的颜色
    先设置ColorDepth、ImageSize属性值再添加图片,反之不能更改这两个属性值

    二.ListView:存储项集合

    Items ListView中的项
    View 指定那种视图显示
    LargeImageList 大图标图像的ImageList控件
    SmallImageList 小图标图像的ImageList控件


    三.ContextMenuStrip

    Items 快捷菜单项的集合
    DisplayStyle 每一项显示的状态(文字,图像)

    在控件上选择ContextMenuStrip属性绑定快捷菜单

    四.动态绑定ListView中的数据

    #region 动态绑定ListView中的数据

    //定位到父项
    ListViewItem itemc = new ListViewItem("C盘:",0);
    //第一种:通过父项.SubItems.Add()添加单个子项
    itemc.SubItems.Add("本地磁盘");
    itemc.SubItems.Add("250GB");
    itemc.SubItems.Add("1KB");

    ListViewItem itemd = new ListViewItem("D盘:",1);
    //方式二:通过父项.SubItems.AddRange()添加多个子项
    itemd.SubItems.AddRange(new string[]{"本地磁盘","1TB","250GB"});


    //最后一步:将父项以及父项的子项集合添加到ListView当中
    this.lvwindows.Items.Add(itemc);
    this.lvwindows.Items.Add(itemd);

    //通过下标定位到父项然后添加子项列表数据
    ListViewItem iteme = this.lvwindows.Items[2];
    iteme.SubItems.AddRange(new string[] { "本地磁盘", "1TB", "250GB" });

    #endregion

    五.动态从数据库获取数据绑定
    string constr = "Data Source=.;Initial Catalog=SchoolDB;User ID=sa;Password=.";
    SqlConnection con = new SqlConnection(constr);
    try
    {
    con.Open();
    string sql = @"select Grade.*,Student.* from Grade,Student where Grade.GradeId=Student.GradeId
    and Student.StudentName like '%"+this.txtName.Text+"%' ";

    SqlCommand com = new SqlCommand(sql,con);
    SqlDataReader reader=com.ExecuteReader();
    //判断读取出来的数据为不为空
    if (reader.HasRows) {
    while(reader.Read()){
    ListViewItem item = new ListViewItem(reader["StudentNo"].ToString());
    item.SubItems.AddRange(new string[] { reader["StudentName"].ToString(), reader["Sex"].ToString(), reader["GradeName"].ToString() });
    item.Tag = (int)reader["StudentNo"];
    this.lvStudentList.Items.Add(item);
    }
    }

    }
    catch (Exception x)
    {
    MessageBox.Show(x.ToString());
    }
    finally {
    con.Close();
    }

    获取选中项的Tag值:this.lvStudentList.SelectedItems[0].Tag.ToString()

  • 相关阅读:
    20200302 数据分析之numpy以及Jupyter
    20200228 scrapy高级使用及分布式
    20200226 请求库selenium
    20200224 爬虫-requests模块
    0219 请求上下文解析与g对象
    Matlab矩阵求导和求梯度有什么不同
    矩阵中范数的处理方式
    如何巧妙地使用递归
    遗传算法求最短路径
    nparray的维度和取值方式
  • 原文地址:https://www.cnblogs.com/bk1234/p/9429795.html
Copyright © 2011-2022 走看看