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()

  • 相关阅读:
    8月的list
    hdu 2853
    【问题交流】JAVA 基础 switch 执行顺序的问题
    h5/css动态旋转木马源码
    javascript系列丛书之读后感
    java运行闪退,报错如下,是因为ole32.dll的问题吗?
    js
    切换为文本框编辑状态,点击空白区域保存修改
    前端工程师源码分享:html5 2d 扇子
    2017武汉马拉松4月9日开跑啦~~~
  • 原文地址:https://www.cnblogs.com/yjc1605961523/p/9434926.html
Copyright © 2011-2022 走看看