LZ还真是懒啊,我就动手给你做个示范。
源码见附件
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
AddData();
}
/// <summary>
/// 执行SQL查询,显示到ListView中
/// </summary>
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);
}
/// <summary>
/// ListView的ItemActivate事件(双击弹出修改窗体)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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();
}
}
}
}
public partial class Form2 : Form
{
ListViewItem oldItem;
/// <summary>
/// 利用ListViewItem参数初始化窗体对象
/// </summary>
/// <param name="listItem"></param>
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;
}
/// <summary>
/// 点击确定,把最新的值赋给ListViewItem对象,由于对象指向的是同一个地址,所以这里一修改,主界面被双击的ListViewItem也被修改了
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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;
}
}
[[it] 本帖最后由 乖老鼠 于 2009-7-27 09:22 编辑 [/it]]