ListView显示列表,双击ListView里任意一行信息,出现一个可以修改的窗口

1、建立窗体Form1,并放一ListView控件

 

2、 ///


        /// 执行SQL查询,显示到ListView中
        ///

        private void AddData()
        {
            //由于这里只做演示,数据就手动添加了
            ListViewItem newItem1 = new ListViewItem(new string[] { "刘德华", "男", "未知", "汉族", "艺人" });
            this.listView1.Items.Add(newItem1);
            ListViewItem newItem2 = new ListViewItem(new string[] { "乖老鼠", "男", "100", "汉族", "程序员" });
            this.listView1.Items.Add(newItem2);
            ListViewItem newItem3 = new ListViewItem(new string[] { "高圆圆", "女", "未知", "不详", "乖老鼠的老婆" });
            this.listView1.Items.Add(newItem3);
        }
3、 listView1_ItemActivate事件

      private void listView1_ItemActivate(object sender, EventArgs e)
        {
            if (listView1.SelectedItems[0].Index != -1)
            {
                ListViewItem oldItem = listView1.SelectedItems[0];
                Form2 ff = new Form2(oldItem); //将这一行ListViewItem作为参数传给另外一个窗体
                if (ff.ShowDialog() == DialogResult.OK)//如果被修改,刷新listView1
                {
                    listView1.Refresh();
                }
            }

        }

4、在Form1的load事件增加

    AddData();

 

 

5、增加一窗体Form2

6、 定义变量 ListViewItem oldItem;

7、重写构造函数

  public Form2(ListViewItem listItem)
        {
            InitializeComponent();
            oldItem = listItem;
            this.textBox1.Text = oldItem.SubItems[0].Text;
            this.textBox2.Text = oldItem.SubItems[1].Text;
            this.textBox3.Text = oldItem.SubItems[2].Text;
            this.textBox4.Text = oldItem.SubItems[3].Text;
            this.textBox5.Text = oldItem.SubItems[4].Text;
        }

8、增加一button

       private void button1_Click(object sender, EventArgs e)
        {
            oldItem.SubItems[0].Text = this.textBox1.Text.Trim();
            oldItem.SubItems[1].Text = this.textBox2.Text.Trim();
            oldItem.SubItems[2].Text = this.textBox3.Text.Trim();
            oldItem.SubItems[3].Text = this.textBox4.Text.Trim();
            oldItem.SubItems[4].Text = this.textBox5.Text.Trim();
            this.DialogResult = DialogResult.OK;

        }

你可能感兴趣的:(c#技巧)