Hello,
I am stuck at the point of updating XML when cell value changed in DataGridView.
I am using LINQ to XML approach. I am able to assign the query output using ToList() as Datasource of DataGridView.
On Cell value changed or Row added/deleted, I tried to cast the Datasource to DataTable & DataSet, but failed on both attempts.
In what form can i get the DataGridView.DataSource, so that i can hit the update on the XML file?
Let me elaborate the situation,
var query = from e in empxmldoc.Descendants("emp")
where (string)e.Attribute("dept") == "dept01";
select new { Name = e.Value, Dept=e.Attribute("dept").Value, Salary=e.Attribute("sal").Value};
DataGridView1.DataSource = query.ToList();
Above illustrates how i am loading my xml data into DataGridView. [Above is working, no issue with loading data into DataGridView]
But, on cell value changed, row added events, I tried
1. DataTable dt = DataGridView1.DataSource;
2. DataSet ds = DataGridView1.DataSource;
3. DataTable dt = (DataTable)DataGridView1.DataSource;
4. DataSet ds = (DataSet)DataGridView1.DataSource;
But, cannot get the data into any of them.
Loading
Leone DamasPosted Feb 18, 2011, 12:52 AM
Thanks. I will try to implement into my code. Shall post back results. Hope it works.
Suthish NairPosted Feb 18, 2011, 12:37 AM
Yes Mamta is right. Or you need to generate dynamic datatable at runtime.
refer example
Mamta MPosted Feb 18, 2011, 12:32 AM
I did quite some research and this is what I found:
You will need to create a class represnting your XML entity (eg: dept or employees, whatever)
Use a BindingList. The BindingList acts similar to List and can be used as a datasource for your DGV, the advantage of BindingList is that it supports two way data binding so that any changes in your UI will reflect in the XML file.
http://msdn.microsoft.com/en-us/library/ms132679%28VS.80%29.aspx#Y509
BindingList
Then do this:
dataGridView1.DataSource = bl;
After this, two way data binding will work perfectly.
Hope this helps.
-Mamta
Leone DamasPosted Feb 17, 2011, 11:44 PM
Are you suggesting to add the query.ToList() to a DataTable, and then use the DataTable as DataSource? That would be great. Then, I think i might be able to recover the DataSource into an DataTable object.
You have any idea how to save the output of LINQ[XML] query variable into DataTable?
PS: Have added elaboration from my 3rd post to the first one to make it more readable
Sam HobbsPosted Feb 17, 2011, 6:22 PM
Is that going in the direction you want to go?
Leone DamasPosted Feb 17, 2011, 4:10 PM
Please go through the previous posts again. You are not able to understand the problem. Problem is not with the part of the code that you are mentioning. Appreciate that you are trying to help.
Suthish NairPosted Feb 17, 2011, 3:09 PM
or trylike this... am assuming empxmldoc as XDocument object
var query = from e in empxmldoc.Descendants("emp")
where e.Attribute("dept").Value == "dept01"
select new { Name = e.Element("name").Value, Dept=e.Element("dept").Value, Salary=e.Element("sal").Value};
DataGridView1.DataSource = query.ToList();
Or upload the xml file.
Leone DamasPosted Feb 17, 2011, 12:26 PM
Glad you got the issue.
Can anyone suggest any way?
Mamta MPosted Feb 17, 2011, 6:34 AM
Leone DamasPosted Feb 17, 2011, 5:00 AM
Let me elaborate the situation,
var query = from e in empxmldoc.Descendants("emp")
where (string)e.Attribute("dept") == "dept01";
select new { Name = e.Value, Dept=e.Attribute("dept").Value, Salary=e.Attribute("sal").Value};
DataGridView1.DataSource = query.ToList();
Above illustrates how i am loading my xml data into DataGridView.
But, on cell value changed, row added events, I tried
1. DataTable dt = DataGridView1.DataSource;
2. DataSet ds = DataGridView1.DataSource;
3. DataTable dt = (DataTable)DataGridView1.DataSource;
4. DataSet ds = (DataSet)DataGridView1.DataSource;
But, cannot get the data into any of them.
Mamta MPosted Feb 17, 2011, 3:37 AM
Sorry I missed out that part. Perhaps this link may be more useful, you can serialize your List to Xml as shown in this link.
http://www.switchonthecode.com/tutorials/csharp-tutorial-xml-serialization
Leone DamasPosted Feb 17, 2011, 3:17 AM
Mamta MPosted Feb 17, 2011, 12:13 AM
http://msdn.microsoft.com/en-us/library/system.data.dataset.writexml(v=VS.90).aspx