I can not answer all your questions, but I think the code below will get you started.
You need a Form with a
DataGridView
and a button on it.
A temporary class is used to store the data suitable for display in a
DataGridView
.
The main
XmlDriver
class also has some routines for serializing to and from XML, but in the example this is not used.
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Forms;
using System.Xml.Serialization;
namespace TestSerialization2
{
public partial class Form2 : Form
{
public BindingList<XmlDriverForGrid> bindingList;
public Form2()
{
InitializeComponent();
}
private void buttonLoadXml_Click(object sender, EventArgs e)
{
bindingList = new BindingList<XmlDriverForGrid>();
this.AddTestData();
}
private void AddTestData()
{
var item = new XmlDriver("Brand1", "Test model1");
item.Xptz = new XmlDriver.Ptz();
var itemForGrid = new XmlDriverForGrid(item);
bindingList.Add(itemForGrid);
item = new XmlDriver("Brand2", "Test model2");
item.Xptz = new XmlDriver.Ptz(true, "Test zoomcommand2");
var itemForGrid2 = new XmlDriverForGrid(item);
bindingList.Add(itemForGrid2);
this.dataGridView1.DataSource = bindingList;
}
public class XmlDriver
{
public XmlDriver()
{
}
public XmlDriver(string brand, string model)
{
this.Xbrand = brand;
this.Xmodel = model;
}
public string Xbrand { get; set; }
public string Xmodel { get; set; }
public Ptz Xptz { get; set; }
public void Save(string FileName)
{
using (var writer = new System.IO.StreamWriter(FileName))
{
var serializer = new XmlSerializer(this.GetType());
serializer.Serialize(writer, this);
writer.Flush();
}
}
public void Load(string FileName)
{
XmlDriver temp;
using (var stream = System.IO.File.OpenRead(FileName))
{
var serializer = new XmlSerializer(typeof(XmlDriver));
temp = serializer.Deserialize(stream) as XmlDriver;
}
this.Xbrand = temp.Xbrand;
this.Xmodel = temp.Xmodel;
this.Xptz = temp.Xptz;
}
public class Ptz
{
public Ptz()
{
}
public Ptz(bool enabled, string zoomCommand)
{
this.Enabled = enabled;
this.ZoomCommand = zoomCommand;
}
public bool Enabled { get; set; }
public string ZoomCommand { get; set; }
}
}
public class XmlDriverForGrid
{
public XmlDriverForGrid(XmlDriver xmlDriver)
{
this.Xbrand = xmlDriver.Xbrand;
this.Xmodel = xmlDriver.Xmodel;
this.Enabled = xmlDriver.Xptz.Enabled;
this.ZoomCommand = xmlDriver.Xptz.ZoomCommand;
}
public string Xbrand { get; set; }
public string Xmodel { get; set; }
public bool Enabled { get; set; }
public string ZoomCommand { get; set; }
}
}
}