tags:

views:

42

answers:

1

I have an existing xml document with music playlist information, which is read into a GridView control in Visual Basic. I am now wanting to save any updates in the GridView to that xml document. How can I do this?

Private Sub cboUsers_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboUsers.SelectedIndexChanged
    Dim songList As New XmlDocument
    songList.LoadXml(s.GetPlaylist(cboUsers.SelectedItem.ToString()))
    Grid.Rows.Clear()
    Dim songs As XmlNodeList = songList.DocumentElement.SelectNodes("//Song")
    Dim song As XmlNode
    For Each song In songs
        Dim artist As String = song.SelectSingleNode("artist").InnerText
        Dim title As String = song.SelectSingleNode("title").InnerText
        Dim length As String = song.SelectSingleNode("length").InnerText
        Dim album As String = song.SelectSingleNode("album").InnerText
        Dim popularity As String = song.SelectSingleNode("popularity").InnerText
        Dim row() As Object = {artist, title, length, album, popularity}
        Grid.Rows.Add(row) ' Add as a row in the Grid
    Next
End Sub

Thanks

A: 

One approach would be to add your own Update command column

<asp:CommandField ShowEditButton="True" HeaderStyle-Width="80px" />

Then use the grids Updating event to do your work. You can either retrieve the new (and old) values from the

GridViewUpdateEventArgs e

Eg

String foo = e.NewValues["columnFoo"].ToString();

Do your update to XML then cancel the update itself

 e.Cancel = true;

C# code examples but easily copied to VB.

Edit, added VB code as requested

Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating

    'Get some new values
    Dim foo As String = e.NewValues("fooColumn").ToString()

    'Im not sure how you plan to find the unique XML node so lets assume that your using title

    'So either get it from a datakey, if your using them in the gridview
    Dim titleFromDataKey As String = GridView1.DataKeys(e.RowIndex)("Title").ToString();

    'Or from the old values
    Dim titleFromOldValues = e.OldValues("Title").ToString()

    'Insert your logic to find the XML node and update it here

    'Cancel the update
    e.Cancel = True

End Sub
Jammin
I'm not familiar with C#, so it isn't so easy to copy to VB for me...could you provide an example in VB please?
weedave
Done, hopefully enough to work with?
Jammin