• 使用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()

  • 相关阅读:
    Codeforces Round #538 (Div. 2) F. Please, another Queries on Array?
    2021 ICPC济南 J Determinant
    牛客小白月赛43 F 全体集合
    The 2021 ICPC Asia Regionals Online Contest (II) L Euler Function
    C++文件操作详解
    利用VC实现图像的特殊显示效果 小楼machine
    Mixed mode assembly is built against version 'v2.0.50727' 解决方案 小楼machine
    如何在对话框资源从一个项目导入到另一个项目使用 Visual c + +.net 或 Visual c + + 2005 小楼machine
    灰度图像阈值化分割常见方法总结及VC实现 小楼machine
    Visual C++多媒体设计及图形、图像处理 小楼machine
  • 原文地址:https://www.cnblogs.com/bk1234/p/9429795.html
Copyright © 2020-2023  润新知