如何将VB中的GridView控件中的更新保存到现有XML文档?

时间:2009-12-14 13:06:35

标签: xml vb.net soap

我有一个带有音乐播放列表信息的现有xml文档,该信息被读入Visual Basic中的GridView控件。我现在想要将GridView中的任何更新保存到该xml文档中。我怎么能这样做?

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

由于

1 个答案:

答案 0 :(得分:0)

一种方法是添加自己的更新命令列

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

然后使用网格更新事件来完成您的工作。您可以从

中检索新的(和旧的)值
GridViewUpdateEventArgs e

例如

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

对XML进行更新然后取消更新

 e.Cancel = true;

C#代码示例,但很容易复制到VB。

编辑,按要求添加VB代码

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