开发者

How do I save updates in a GridView control in VB to an existing XML document?

开发者 https://www.devze.com 2022-12-14 11:04 出处:网络
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 c

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("lengt开发者_如何学Goh").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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消