Reputation: 567
Alright, so I've seen a couple different posts for this subject and each one has a weird and separate answer, but none tailored to my situation:
I have a DataGridView that is populated one row at a time through auser interface and button clicks (no other data sources). Once the DGV has been filled to the user's contentment, they can click a button that should save the current DGV data to an XML file.
I tried doing this through a DataSet or DataTable, but file creation is still a newer concept to me and I don't think I'm seeing the whole picture to get what I'm doing wrong.
The first block of code is what brings the user-input data from the user window to the DGV:
//Function for the Add Car button to open the Add New Car window
private void buttonAddCar_Click(object sender, EventArgs e)
{
AddNewCar displayNewCarWindow = new AddNewCar();
displayNewCarWindow.ShowDialog();
dataGridViewMainCarDisplay.Rows.Add(displayNewCarWindow.NewTrainName
, displayNewCarWindow.NewTrainWidth
, displayNewCarWindow.NewTrainHeight
, displayNewCarWindow.NewTrainLength
, displayNewCarWindow.NewTrainDeadWeight
, displayNewCarWindow.NewTrainLoadCapacity);
}
The second block of code is where I am having issues taking the now-completed DGV data and packaging it up to send to a XML:
/* Function for the Load XML button to send all data populated
in the DataGridView to an XML file */
private void buttonLoadXML_Click(object sender, EventArgs e)
{
//Create a file path and name to store the serialized data
string fileName = @"C:\Users\a.watts\Documents\Visual Studio 2015\Projects\SerializationTrainTypeDemo\XMLRecording\data.xml";
//Create a dataTable to bind the DataGridView to for serialization
DataTable dataTable = new DataTable();
//Bind the DataGridView to the dataTable
for (int i = 0; i < dataGridViewMainCarDisplay.Rows.Count; i++)
{
//Some stuff here to save each DataGridView row one at a time
}
//Write the DataGridView data in the dataTable to an XML
dataTable.WriteXml(fileName);
}
Upvotes: 1
Views: 2547
Reputation:
Ok, here are the pieces that you are looking for.
Bind a datatable to a dataGridView. https://msdn.microsoft.com/en-us/library/fbk67b6z%28v=vs.110%29.aspx
Here is a simple example of adding columns to a DataTable and binding to a data grid view.
dataGridView1.AutoGenerateColumns = true;
dataGridView1.AllowUserToAddRows = true;
DataTable gridTable = new DataTable();
gridTable.Columns.Add("EmployeeName", typeof(string));
gridTable.Columns.Add("StartDate", typeof(DateTime));
gridTable.Columns.Add("Salary", typeof(double));
gridTable.Columns.Add("Location", typeof(string));
dataGridView1.DataSource = gridTable;
When data is added to the grid, it will be added to the datatable. Then you need to serialize the datatable to an XML file. Later, you will probably want to deserialize the XML file back to a datatable. In the examples below, you will change Models.ProjectModel to the object that you are working with - DataTable.
public class ProjectDataAccess : IProjectDataAccess
{
public Models.ProjectModel Get(string filePath)
{
Models.ProjectModel project = new Models.ProjectModel();
// Construct an instance of the XmlSerializer with the type
// of object that is being deserialized.
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Models.ProjectModel));
// To read the file, create a FileStream.
FileStream fileStream = new FileStream(filePath, FileMode.Open);
// Call the Deserialize method and cast to the object type.
project = (Models.ProjectModel)xmlSerializer.Deserialize(fileStream);
fileStream.Close();
return project;
}
public void Save(Models.ProjectModel project, string filePath)
{
System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(project.GetType());
StreamWriter streamWriter = new StreamWriter(filePath);
xmlSerializer.Serialize(streamWriter, project);
streamWriter.Close();
}
}
Upvotes: 1