populate a DataSet via a LINQ query (Linq to XML)

A

Anthony

Is there a way to populate a DataSet via a LINQ query?

(Linq to XML)

Instead of;
DataSet ds = new DataSet();
ds.ReadXml(myfile.xml, XmlReadMode.InferTypedSchema);
dataGridView1.DataSource = ds.DefaultViewManager;
dataGridView1.DataMember = "myNode";


Anthony
 
M

Mr. Arnold

Anthony said:
Is there a way to populate a DataSet via a LINQ query?

(Linq to XML)

Instead of;
DataSet ds = new DataSet();
ds.ReadXml(myfile.xml, XmlReadMode.InferTypedSchema);
dataGridView1.DataSource = ds.DefaultViewManager;
dataGridView1.DataMember = "myNode";


Anthony

You use a Linq-2-XML query and just bind the Linq query results to the
control. You can do that. You don't need a dataset.
 
A

Anthony

You use a Linq-2-XML query and just bind the Linq query results to the
control. You can do that. You don't need a dataset.

How can i bind the Linq query results to the control ?

Anthony
 
M

Mr. Arnold

Anthony said:
I still have a problem.
I've a windows form application and DataGridView does not contain a
definition for 'DataBind' and no extension method 'DataBind'.


XDocument xmlDoc = XDocument.Load(@"c:sites.xml");
var q = from c in xmlDoc.Descendants("site")
where c.Attribute("technical").Value == "true"
select new {
name = c.Element("name").Value,
url = c.Element("url").Value
};

All you have to do here is this.
dataGridView1.DataSource = q;

I think it will bind to the List<T> 'q'. 'q' is a collection of objects

You may look into BindingSource control still using BS.Datasource = q;
 
A

Anthony

[ . . ]
dataGridView1.DataSource = q;

This does not work
You may look into BindingSource control still using BS.Datasource = q;

Tried this:
bindingSource1.DataSource = q;
and set the DataGrid's datasource to bindingSource1

This does not work either.

Anthony
 
M

Mr. Arnold

Anthony said:
[ . . ]
dataGridView1.DataSource = q;

This does not work
You may look into BindingSource control still using BS.Datasource = q;

Tried this:
bindingSource1.DataSource = q;
and set the DataGrid's datasource to bindingSource1

This does not work either.

Anthony

I copied the xml for sites to a file on the HD from the link I provided
you.

I ran this code and it showed the data in the dataGridView.

private void Form1_Load(object sender, EventArgs e)
{

XDocument xmlDoc = XDocument.Load(@"d:\\duane1\\sites.xml");

var q = from c in xmlDoc.Descendants("site")
where c.Attribute("technical").Value == "true"
select new
{
Name = c.Element("name").Value,
URL = c.Element("url").Value
};

var bs = new BindingSource {DataSource = q};

dataGridView1.AutoGenerateColumns = true;
dataGridView1.AutoSize = true;

dataGridView1.DataSource = bs;
}
 
A

Anthony

var bs = new BindingSource {DataSource = q};
dataGridView1.AutoGenerateColumns = true;
dataGridView1.AutoSize = true;

dataGridView1.DataSource = bs;
}

That's another trick (with a few more lines of code) :))))

Do you know why i now can't sort the DataGrid by clicking the column header?
By default, users can sort the data in a DataGridView control by clicking
the header of a column.

Thank you very much !

Anthony
 
M

Mr. Arnold

Anthony said:
That's another trick (with a few more lines of code) :))))

Do you know why i now can't sort the DataGrid by clicking the column header?
By default, users can sort the data in a DataGridView control by clicking
the header of a column.

Thank you very much !

I am more of a Web.UI.Control programmer rather that Windows.UI.Control
programmer. So, I don't know why the sort by column header doesn't work
for you.

You may have to do it manually if someone can't help you to make it work
automatically.

http://msdn.microsoft.com/en-us/library/0868ft3z.aspx

You can also consider this event.

ColumnHeaderMouseClick
This event is used to capture when a header is clicked.
Sometimes you may need to modify the direction that the
sort glyph is pointing in the column squares.
You can check e.ColumnIndex on the event parameter to
find out which of the column headers was clicked.

http://dotnetperls.com/datagridview-tips

You might use this event, and based on e.ColumnIndex, query and sort 'q'
on corresponding property in the list of objects in 'q' and rebind.

If(e.ColumnIndex == 0)
{
var q1 = (from a in q.OrderBy(b => b.Name) select a).tolist();
// bind q1
}

If you want descending then q.OrderByDescending(b => b.Name).

HTH
 
A

Anthony

I am more of a Web.UI.Control programmer rather that Windows.UI.Control
programmer. So, I don't know why the sort by column header doesn't work
for you.

You may have to do it manually if someone can't help you to make it work
automatically.

http://msdn.microsoft.com/en-us/library/0868ft3z.aspx

You can also consider this event.

ColumnHeaderMouseClick
This event is used to capture when a header is clicked.
Sometimes you may need to modify the direction that the
sort glyph is pointing in the column squares.
You can check e.ColumnIndex on the event parameter to
find out which of the column headers was clicked.

http://dotnetperls.com/datagridview-tips

You might use this event, and based on e.ColumnIndex, query and sort 'q'
on corresponding property in the list of objects in 'q' and rebind.

If(e.ColumnIndex == 0)
{
var q1 = (from a in q.OrderBy(b => b.Name) select a).tolist();
// bind q1
}

If you want descending then q.OrderByDescending(b => b.Name).

HTH

Thank you for the explanation.

But it is weird that the default sort by clicking the header of a column
disappeared.
I've checked all settings again, but no result.

Anthony
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top