using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
namespace ListView控件中实现修改
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Load += new EventHandler(Form_Load);
listView1.AfterLabelEdit += new LabelEditEventHandler(ListView_AfterModify);
}
private void Form_Load(object sender, EventArgs e)
{
listView1.Columns.Add("产品名称", 100, HorizontalAlignment.Left);
listView1.Columns.Add("产品说明", 200, HorizontalAlignment.Center);
listView1.Columns.Add("产品数量", 200, HorizontalAlignment.Center);
List<string> list = new List<string>();
list.Add("五叶神");
list.Add("中华");
list.Add("中南海");
Random ram=new Random();
for (int i = 0; i < list.Count; i++)
{
ListViewItem lv = new ListViewItem(list[i]);
lv.SubItems.Add(list[i] + "的说明");
lv.SubItems.Add(ram.Next(300).ToString());
listView1.Items.Add(lv);
}
listView1.LabelEdit = true;
}
private void ListView_AfterModify(object sender, LabelEditEventArgs e)
{
string xmlpath = AppDomain.CurrentDomain.BaseDirectory;
xmlpath = Path.Combine(xmlpath, "temp.xml");
XmlTextWriter xtw = new XmlTextWriter(xmlpath,Encoding.UTF8);
xtw.Formatting = Formatting.Indented;
xtw.WriteStartDocument();
xtw.WriteStartElement("productinfos");
for (int i = 0; i < listView1.Items.Count; i++)
{
ListViewItem item=listView1.Items[i];
xtw.WriteStartElement("product");
xtw.WriteElementString("proname", item.Text);
xtw.WriteElementString("info", item.SubItems[1].Text);
xtw.WriteElementString("qty", item.SubItems[2].Text);
xtw.WriteEndElement();
}
xtw.WriteEndElement();
xtw.WriteEndDocument();
xtw.Close();
}
}
}
ListView创建列和设置数据并保存成xml
最新推荐文章于 2021-05-28 02:43:32 发布