Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Vb.net [2008] LINQ xml query - anonymous type problems

Status
Not open for further replies.

rw409168

Programmer
Jul 16, 2009
95
GB
Greetings,

I'm out of my depth populating a windows form from an XML linq query (see form code below).

The listbox is populated as intended, when a value is selected (row) I would like to populate the remaining form controls.

The linq query I believe has created a new datatype "Anonymous Types".

The query is only local to the load subroutine but I'm not sure how to make it global.

When I define query simply as an object the code in the selectedValueChanged event can't execute due to late databinding.

Can anyone please advice how I can initially load the listbox from linq xml query and then populate the other controls when the value changes?

Am I right in thinking even though the listbox is multi-columns (property) you can ONLY ever reference a row and NOT a column?

I'm new to LINQ and Anonymous Types and the more I read the more confused I seem to get.

On a less important note the order by is not functioning as intended (no errors) but data is always retrieved in xml file order and not by the node <text>.

Also looked at using <text>.first to order the results without success.

Code:
Public Class frmEditQuote

    Private cQuoteFileName As String    'Name of the quote file

    Public Sub New(ByVal quotefile As String)

        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        cQuoteFileName = quotefile

    End Sub

    Private Sub frmEditQuote_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'Validation over use XML LINQ query to find data and populate datagridview
        'if no data is found then inform user via messagebox
        Dim doc As XDocument
        doc = XDocument.Load(cQuoteFileName)

        Dim query = (From quoteval In doc.<quotefile>.<quote> _
        Let quoteordertext = quoteval.<quotefile>.<quote>.<text>.Value _
        Order By quoteordertext _
        Select quoteDate = quoteval.<date>.Value, _
               author = quoteval.<author>.Value, _
               category = quoteval.<category>.Value, _
               quotetext = quoteval.<text>.Value).ToList

        Dim quotebs As New BindingSource
        quotebs.DataSource = query
        lstQuotes.DataSource = quotebs
        lstQuotes.DisplayMember = "quotetext"

      End Sub

    Private Sub lstQuotes_SelectedValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstQuotes.SelectedValueChanged

        'Set quote details into editable controls
        dtpQuoteDate.Value = CDate(query.Item(lstQuotes.SelectedIndex).quoteDate)
        txtAuthor.Text = query.Item(lstQuotes.SelectedIndex).author
        txtQuote.Text = query.Item(lstQuotes.SelectedIndex).quotetext

    End Sub

End Class

Thanks
Rob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top