Hi I’m trying to use a DataGridView for the first time and with LINQ.
Here’s what I’m trying to do: I want to use it (although it doesn’t have to the DataGridView ) to read and display the contents of an XML file (this bit is working code below) but I want to have an add row button on the form or in the DataGridView that takes the contents of three textboxes and and populates the contents of three columns of the new row. The new row is to be appended to the existing data.
Next I would like to add a Delete button to delete the currently selected row.
And lastly I want a save button that exports the contents of the DataGridView back to an XML file overwriting/updating the existing XML file.
So I stuck at adding the additional data! but as I don't currently have a clue as to the delete or save either I thought I would ask all in one go!!
So this is the code I have to read the xml file:
XDocument xmlDoc = XDocument.Load(@"queues.xml");
var q = from c in xmlDoc.Root.Descendants("Queue")
select new
{
QueueNumber = c.Element("Number").Value,
QueueName = c.Element("Name").Value,
QueuePCC = c.Element("QueueTag").Value
};
dataGridView1.DataSource = q.ToList();
XML document:
<?xml version="1.0" encoding="utf-8" ?>
<Queues>
<Queue>
<Number>
001
</Number>
<Name>
mytest
</Name>
<QueueTag>
xyz
</QueueTag>
</Queue>
<Queue>
<Number>
002
</Number>
<Name>
Adi2
</Name>
<QueueTag>
ABCD
</QueueTag>
</Queue>
</Queues>
ok I have now changed my code to this:
XDocument xmlDoc = XDocument.Load(@"queues.xml");
var q = from c in xmlDoc.Root.Descendants("Queue")
select new Queue
{
Number = c.Element("Number").Value,
Name = c.Element("Name").Value,
QueueTag= c.Element("QueueTag").Value
};
var queryAsList = new BindingList<Queue>(q.ToList());
bindingSource1.DataSource = queryAsList;
dataGridView1.DataSource = bindingSource1;
This allows me to add and delete rows and data from the dataGridView :)
But I can still find no way to writing the data back to XML either from the dataGridView or from the bindingSource1 :(
Any help please? the bindingSource1.count changes each time I make a change to the data so I think I'm close!