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

  • 相关阅读:
    支付宝支付内容 尚未完成
    mvc 高并发解决方案之一---存储过程
    微信第三方平台开头篇--MVC代码(第三方获取ticket和公众号授权)
    卡券类字段
    Jquery 方法学习
    c# 对象反射赋值未知属性需类型转换
    C# enum 枚举 反射
    MVC c# 调用sql的存储过程
    Javascript装饰器原理
    关于阿里云的产品应用思考
  • 原文地址:https://www.cnblogs.com/chx9832/p/9429606.html
Copyright © 2020-2023  润新知