简体   繁体   中英

C# Write/Read GridDataView to XML?

I created a DataGridView and I Configured it! how I can save all what the user input in XML file (settings.xml)? so next time the user run the program it will read all the data and view it in the GridDataView

the data will not be that much, it is just some kind of settings!

I found allot of tutorials online, but they either does not work or using DataGrid !!!

Edit: is this really hard to do! I notice that people who ask this questions do not get a solution, in spite of the hard work the experts do to explain !!!!!

I want to learn it any link to a tutorial (That Works)?

Easiest way is to connect a DataSet as datasource and then save the dataset to xml and load the xml the next time.

    DataSet ds = new DataSet();
    //save the dataset as xml
    ds.WriteXml("your path to save the xml");

    //read the xml into your dataset
    ds.ReadXml("your path to save the xml");

You need to serialize your DataSource not DataGrid.

Create new DataTable, add columns, bound DataTable to DataGrid. Then use DataTable.WriteXml and DataTable.ReadXml to save and load xml.

the most easy way:

Private DataSet UserSettings(string pathofXML)
{
   DataSet ds = new DataSet();
   ds.ReadXml(pathofXML);
   return ds;
}

private void BindGrid()
{
   string pathOfXML = Application.StartupPath + @"\Settings.xml";
   gridview.DataSource = UserSettings(pathOfXML);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM