C# winform中ListView用法

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@22

           listView1.View = View.Details;//listView显示方式
            listView1.Columns.Add("进程名");//增加项目标题
            listView1.Columns.Add("路径");//增加项目标题
            listView1.Columns.Add("进程名");//增加项目标题
            //添加一行项目
            listView1.Items.Add("name", "QQ.exe", 0);
            listView1.Items["name"].SubItems.Add("c:\\QQ.exe");
            listView1.Items["name"].SubItems.Add("pin");

//@@@@@@@@@@@@第二种方式
            listView1.BeginUpdate();
            this.listView1.GridLines = true; //显示表格线
            this.listView1.FullRowSelect = true;//是否可以选择行 
            this.listView1.Scrollable = true;//有滚动条

            listView1.View = View.Details;//listView显示方式
            listView1.Columns.Add("文件名",100);//创建标题
            listView1.Columns.Add("大小",50);
            listView1.Columns.Add("創建日期",150);
          
            ListViewItem lvItem = new ListViewItem();// 定义添加第一个项目
            lvItem.Text = "文件夾1";
            listView1.Items.Add(lvItem);

            ListViewItem.ListViewSubItem lvSubItem = new ListViewItem.ListViewSubItem();//定义第一个子项目
            lvSubItem.Text = "10";
            lvItem.SubItems.Add(lvSubItem);

            lvSubItem = new ListViewItem.ListViewSubItem();//添加第二个子项目
            lvSubItem.Text = "20080114";
            lvItem.SubItems.Add(lvSubItem);

            lvItem = new ListViewItem();//添加第二个项目
            lvItem.Text = "文件夾2";
            lvSubItem = new ListViewItem.ListViewSubItem();
            lvSubItem.Text = "20";
            lvItem.SubItems.Add(lvSubItem);
            lvSubItem = new ListViewItem.ListViewSubItem();
            lvSubItem.Text = "20080115";
            lvItem.SubItems.Add(lvSubItem);
            listView1.Items.Add(lvItem);
            listView1.EndUpdate();

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

/更改属
            this.listView1.GridLines = true; //显示表格线
            this.listView1.View = View.Details;//显示表格细节
            this.listView1.LabelEdit = true; //是否可编辑,ListView只可编辑第一列。
            this.listView1.Scrollable = true;//有滚动条
            this.listView1.HeaderStyle = ColumnHeaderStyle.Clickable;//对表头进行设置
            this.listView1.FullRowSelect = true;//是否可以选择行

            //this.listView1.HotTracking = true;// 当选择此属性时则HoverSelection自动为true和Activation属性为oneClick
            //this.listView1.HoverSelection = true;
            //this.listView1.Activation = ItemActivation.Standard; //
            //添加表头
            this.listView1.Columns.Add("", 0);
            this.listView1.Columns.Add("列1",80);
            this.listView1.Columns.Add("列2", 160);
            //添加各项
            ListViewItem[] p = new ListViewItem[2];
            p[0] = new ListViewItem(new string[] { "","aaaa","bbbb"});
            p[1] = new ListViewItem(new string[] { "","cccc", "ggggg" });
            //p[0].SubItems[0].BackColor = Color.Red; //用于设置某行的背景颜色

            this.listView1.Items.AddRange(p);
            //也可以用this.listView1.Items.Add();不过需要在使用的前后添加Begin... 和End...防止界面自动刷新
            // 添加分组
            this.listView1.Groups.Add(new ListViewGroup("tou"));
            this.listView1.Groups.Add(new ListViewGroup("wei"));

            this.listView1.Items[0].Group = this.listView1.Groups[0];
            this.listView1.Items[1].Group = this.listView1.Groups[1];

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

           //实现选中某行右键弹出菜单,添加contextmenustrip控件,

            //listView1.ContextMenuStrip = this.contextMenuStrip1;

           //添加鼠标右击弹出菜单,使listview与contextMenu连接

            if (listView1.SelectedItems.Count > 0)
            {
                ListViewItem item = listView1.SelectedItems[0];
                string filename = item.Text;
                //string pName = item.SubItems[1].Text;
                //MessageBox.Show (String, String, MessageBoxButtons, MessageBoxIcon) 
                DialogResult dr = MessageBox.Show(string.Format("确定要删除 {0}文件吗?", filename),
                    "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (dr == DialogResult.OK)
                {
                    try
                    {
                        File.Delete(filename);
                        
                    }
                    catch
                    {
                        MessageBox.Show("文件删除失败");
                    }

                }
            }

posted @ 2010-12-07 15:03  ccczqh  阅读(3071)  评论(0编辑  收藏  举报