给你个我以前写的例子参考下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;using System.Xml;
namespace XMLReadAndStore
{
public partial class Form1 : Form
{
private static readonly string FileName = "DomainSystem.xml"; DataSet set = new DataSet();
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
DataGridViewBind();
}
private void DataGridViewBind()
{
try
{
DataTable table = new DataTable("item");
table.Columns.AddRange(new DataColumn[]
{
new DataColumn("id"),
new DataColumn("DomainName"),
new DataColumn("DomainDescribe"),
new DataColumn("DomainRegDate"),
new DataColumn("DomainDeadDate")
});
table.Columns["id"].DataType = typeof(Int32);
table.ReadXml(FileName); //读取xml文件
set.Tables.Add(table);
dataGridView1.DataSource = set.Tables[0];//设置dataGridView的数据源
}
catch (IndexOutOfRangeException)
{
MessageBox.Show("Cannot find table 0 .");
}
} private void btnAdd_Click(object sender, EventArgs e)
{
DataTable table = set.Tables[0]; DataRow[] rows = table.Select(@"DomainName = '" + this.txtName.Text.Replace("'", "''") + "'"); rows = table.Select(@"id = max(id)");
int x = Convert.ToInt32(rows[0]["id"].ToString());
DataRow row = table.NewRow();
row["id"] = x + 1;
row["DomainName"] = txtName.Text;
row["DomainDescribe"] = txtDescribe.Text;
row["DomainRegDate"] = dateTimePicker1.Value;
row["DomainDeadDate"] = dateTimePicker2.Value;
table.Rows.Add(row);
set.WriteXml(FileName);
} private void button1_Click(object sender, EventArgs e)
{
} private void btnDel_Click(object sender, EventArgs e)
{
//foreach (DataGridViewRow row in dataGridView1.SelectedRows) // 与下面的功能相同
//{
// dataGridView1.Rows.Remove(row);
// set.WriteXml(FileName);
//} for (int i = 0; i < this.dataGridView1.SelectedRows.Count; i++)
{
this.dataGridView1.Rows.RemoveAt(this.dataGridView1.CurrentRow.Index);
set.WriteXml(FileName);
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;using System.Xml;
namespace XMLReadAndStore
{
public partial class Form1 : Form
{
private static readonly string FileName = "DomainSystem.xml"; DataSet set = new DataSet();
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
DataGridViewBind();
}
private void DataGridViewBind()
{
try
{
DataTable table = new DataTable("item");
table.Columns.AddRange(new DataColumn[]
{
new DataColumn("id"),
new DataColumn("DomainName"),
new DataColumn("DomainDescribe"),
new DataColumn("DomainRegDate"),
new DataColumn("DomainDeadDate")
});
table.Columns["id"].DataType = typeof(Int32);
table.ReadXml(FileName); //读取xml文件
set.Tables.Add(table);
dataGridView1.DataSource = set.Tables[0];//设置dataGridView的数据源
}
catch (IndexOutOfRangeException)
{
MessageBox.Show("Cannot find table 0 .");
}
} private void btnAdd_Click(object sender, EventArgs e)
{
DataTable table = set.Tables[0]; DataRow[] rows = table.Select(@"DomainName = '" + this.txtName.Text.Replace("'", "''") + "'"); rows = table.Select(@"id = max(id)");
int x = Convert.ToInt32(rows[0]["id"].ToString());
DataRow row = table.NewRow();
row["id"] = x + 1;
row["DomainName"] = txtName.Text;
row["DomainDescribe"] = txtDescribe.Text;
row["DomainRegDate"] = dateTimePicker1.Value;
row["DomainDeadDate"] = dateTimePicker2.Value;
table.Rows.Add(row);
set.WriteXml(FileName);
} private void button1_Click(object sender, EventArgs e)
{
} private void btnDel_Click(object sender, EventArgs e)
{
//foreach (DataGridViewRow row in dataGridView1.SelectedRows) // 与下面的功能相同
//{
// dataGridView1.Rows.Remove(row);
// set.WriteXml(FileName);
//} for (int i = 0; i < this.dataGridView1.SelectedRows.Count; i++)
{
this.dataGridView1.Rows.RemoveAt(this.dataGridView1.CurrentRow.Index);
set.WriteXml(FileName);
}
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货