13
Reply

Update to XML using DataGridView (LINQ)

Leone Damas

Leone Damas

Feb 16 2011 3:20 PM
9.1k
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.


Answers (13)