如何从datagridview更新XML文件 [英] How to update the XML file from datagridview
问题描述
我很无奈plz帮我这个我有datagridview和xml文件如果我对datagridview做了任何修改那么修改必须在xml文件中我不知道如何访问节点和desecendents。
我的尝试:
if(e.ColumnIndex == 10)
{
XDocument xdoc = XDocument.Load(xml.xml);
DataGridViewRow data = dataGridView1。行[1];
data.Cells [0] .Value = dataGridView1.Rows [e.RowIndex] .Cells [0] .Value;
data.Cells [ 1] .Value = dataGridView1.Rows [e.RowIndex] .Cells [1] .Value;
data.Cells [2] .Value = dataGridView1.Rows [e.RowIndex] .Cells [2 ] .Value;
data.Cells [3] .Value = dataGridView1.Rows [e.RowIndex] .Cells [3] .Value;
data.Cells [4 ] .Value = dataGridView1.Rows [e.Row索引] .Cells [4] .Value;
data.Cells [5] .Value = dataGridView1.Rows [e.RowIndex] .Cells [5] .Value;
data.Cells [6] .Value = dataGridView1.Rows [e.RowIndex] .Cells [6] .Value;
data.Cells [7] .Value = dataGridView1.Rows [e.RowIndex ] .Cells [7] .Value;
data.Cells [8] .Value = dataGridView1.Rows [e.RowIndex] .Cells [8] .Value;
data.Cells [9] .Value = dataGridView1.Rows [e.RowIndex] .Cells [9] .Value;
xdoc.Save(xml3.xml);
i was helpless plz help me with this i have datagridview and xml file if i did any modification to datagridview then the modifications has to be there in xml file i dont know how to access the nodes and desecendents.
What I have tried:
if (e.ColumnIndex == 10)
{
XDocument xdoc = XDocument.Load("xml.xml");
DataGridViewRow data=dataGridView1.Rows[1];
data.Cells[0].Value = dataGridView1.Rows[e.RowIndex].Cells[0].Value;
data.Cells[1].Value = dataGridView1.Rows[e.RowIndex].Cells[1].Value;
data.Cells[2].Value =dataGridView1.Rows[e.RowIndex].Cells[2].Value;
data.Cells[3].Value = dataGridView1.Rows[e.RowIndex].Cells[3].Value;
data.Cells[4].Value = dataGridView1.Rows[e.RowIndex].Cells[4].Value;
data.Cells[5].Value =dataGridView1.Rows[e.RowIndex].Cells[5].Value;
data.Cells[6].Value = dataGridView1.Rows[e.RowIndex].Cells[6].Value;
data.Cells[7].Value = dataGridView1.Rows[e.RowIndex].Cells[7].Value;
data.Cells[8].Value =dataGridView1.Rows[e.RowIndex].Cells[8].Value;
data.Cells[9].Value = dataGridView1.Rows[e.RowIndex].Cells[9].Value;
xdoc.Save("xml3.xml");
推荐答案
我建议使用 BindingSource ,这是一篇很好的文章:详细的数据绑定教程 [ ^ ]
示例( e):
I would recommend using a BindingSource, here is an excellent article about it: A Detailed Data Binding Tutorial[^]
example (quote):
BindingSource bs = new BindingSource(); //**
private void Form1_Load(object sender, EventArgs e)
{
bs.DataSource = typeof(Airplane); //**
bs.Add(new Airplane("Boeing 747", 800));
bs.Add(new Airplane("Airbus A380", 1023));
bs.Add(new Airplane("Cessna 162", 67));
grid.DataSource = bs; //**
grid.AutoGenerateColumns = true; // create columns automatically //**
txtModel.DataBindings.Add("Text", bs, "Model"); //**
}
这里使用了一个类,但你可能想要使用 List<>
这是完全可能的。
要保存您的类或列表,您可以使用序列化。
Here a class is used, but you probably want to use a List<>
which is perfectly possible.
To save your class or List, you could use serialization.
目前还不清楚数据的格式,所以这里有一个例子:
It is unclear what format the data is in so here is an example for you:
<dataset>
<employee>
<name>Fred</name>
</employee>
</dataset>
和加载的代码&保存...
And the code to load & save...
private void Load()
{
string path = @".\dataset.xml";
DataSet ds = new DataSet();
ds.ReadXml(path);
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "employee";
}
private void Save()
{
string path = @".\dataset.xml";
DataSet ds = (DataSet) dataGridView1.DataSource;
ds.WriteXml(path);
}
这篇关于如何从datagridview更新XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!