Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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");
Posted

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");                  //**
}
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.
 
Share this answer
 
v2
It is unclear what format the data is in so here is an example for you:
XML
<dataset>
  <employee>
    <name>Fred</name>
  </employee>
</dataset>

And the code to load & save...
C#
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);
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900