.NET Framework - populate a DataSet via a LINQ query (Linq to XML)

Asked By Anthony
13-Apr-10 12:21 PM
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
XmlReadMode
(1)
OrderByDescending
(1)
EventArgs
(1)
XDocument
(1)
DataGrid
(1)
LINQ
(1)
ColumnHeaderMouseClick
(1)
Attribute
(1)
  Mr. Arnold replied to Anthony
13-Apr-10 12:39 PM
You use a Linq-2-XML query and just bind the Linq query results to the
control. You can do that. You do not need a dataset.
  Anthony replied to Mr. Arnold
13-Apr-10 01:10 PM
How can i bind the Linq query results to the control ?

Anthony
  Mr. Arnold replied to Anthony
13-Apr-10 01:56 PM
it is in the link.

http://blogs.techrepublic.com.com/programming-and-development/?p=594
  Anthony replied to Mr. Arnold
16-Apr-10 02:36 AM
I still have a problem.
I have a windows form application and DataGridView does not contain a
definition for 'DataBind' and no extension method 'DataBind'.

Anthony
  Mr. Arnold replied to Anthony
16-Apr-10 03:56 AM
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;
  Anthony replied to Mr. Arnold
16-Apr-10 01:12 PM
[ . . ]


This does not work


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

This does not work either.

Anthony
  Martin Honnen replied to Anthony
16-Apr-10 01:27 PM
Try
dataGridView1.DataSource = q.ToList();



--

Martin Honnen --- MVP Data Platform Development
http://msmvps.com/blogs/martin_honnen/
  Anthony replied to Martin Honnen
16-Apr-10 02:27 PM
That's the trick :-))))

Thank you very much !

Anthony
  Mr. Arnold replied to Anthony
16-Apr-10 02:35 PM
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;
}
  Anthony replied to Mr. Arnold
17-Apr-10 03:24 AM
That's another trick (with a few more lines of code)    :-))))

Do you know why i now cannot 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
  Mr. Arnold replied to Anthony
17-Apr-10 09:26 AM
I am more of a Web.UI.Control programmer rather that Windows.UI.Control
programmer. So, I do not know why the sort by column header does not work
for you.

You may have to do it manually if someone cannot 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
  Anthony replied to Mr. Arnold
19-Apr-10 01:49 AM
Thank you for the explanation.

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

Anthony
Create New Account
help
in the same form Public Event select_Click(ByVal sender As Object, ByVal e As System.EventArgs) Public Event new_Click(ByVal sender As Object, ByVal e As System.EventArgs) Public Event edit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Public Event delete_Click(ByVal sender As Object, ByVal e As System EventArgs) Public Event prior_Click(ByVal sender As Object, ByVal e As System.EventArgs) Public Event next_Click(ByVal sender As Object, ByVal e As System.EventArgs) Public Event print_Click(ByVal sender As Object, ByVal e As System.EventArgs) Public Event xls_Click(ByVal sender As Object, ByVal e As System EventArgs) Public Event fax_Click(ByVal sender As Object, ByVal e As System.EventArgs) Public Event enquiry_Click
String.Empty; private Favorite theFavorites = new Favorite(); public Form1() { InitializeComponent(); } private void Form1_load(object sender, EventArgs e) { this.theFavorites = Favorite.DoLoadFavorites(); this.favoriteToolStripMenuItem.DropDown.Items.Clear(); / / clear the items that already Windows.Forms.Application.StartupPath + theSerializer.Serialize(theWriter, this); theWriter.Close(); } } private void webBrowser1_DocumentTitleChanged(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { webBrowser1.Navigate(textBox1.Text); } private void button2_Click(object sender, EventArgs e) { webBrowser1.GoBack(); } private void button3_Click(object sender, EventArgs e) { webBrowser1.GoForward(); } private void button4_Click(object sender EventArgs e) { webBrowser1.Refresh(); } private void printToolStripMenuItem_Click(object sender, EventArgs e) { webBrowser1.ShowPrintDialog(); } private void printPreviewToolStripMenuItem_Click(object sender, EventArgs e) { webBrowser1.ShowPrintPreviewDialog(); } private void refreshToolStripMenuItem_Click(object sender, EventArgs e) { webBrowser1.Refresh(); } private void button5_Click
KB / cs / event_fundamentals.aspx one should, if possible, use the generic framework supplied objects System.EventArgs of a class that derives from System.EventArgs." and support for it is built into the .NET Framework through the System.EventArgs class." - -and- - to), you should use one of the EventHandler delegates provided by the .NET following simple form button_click wants to raise an event that carries no data (so System.EventArgs.Empty) namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public delegate void EventHandler(object sender, EventArgs e); public event EventHandler MyEvent; public class MyEventArgs : System.EventArgs { } public void RaiseTheEvent(MyEventArgs eventArgs) { try { EventHandler handler = MyEvent; if (handler ! = null) { handler(this, eventArgs); } } catch { / / Handle exceptions here } } private void button1_Click(object sender, EventArgs e) { RaiseTheEvent(MyEventArgs.Empty); } } } . . .useless
marked with * below how would that / / Create a delegete public delegete void MyEventHandler(object sender, EventArgs e); / / Create an event member public event MyEventHandler MyEvent; / / Invoke the delegete within a method need to raise the event, as the following code demonstrates if(MyEvent ! = null) MyEvent((this, EventArgs.Empty) EventArgs e = new EventArgs(); if (handler ! = null) handler(this, e); I do not see the * !?!? Arne Sorry I mean mean something like this? - -- -- MyEvent = new MyEventHandler( (sender_, e_) = > { throw new NotImplementedException(); }); MyEvent.Invoke(this, EventArgs.Empty); - -- -- Or just. . . - -- -- if (MyEvent ! = null) { MyEvent.Invoke(this, EventArgs.Empty); } - -- -- In the book I read it says the following. How to raise an event need to raise the event, as the following code demonstrates if (MyEvent ! = null) MyEvent(this, EventArgs.Empty); EventArgs e = new EventArgs(); if (handler ! = null) { / / invoke the delegete handler(this ! = null); } I understand everything