简体   繁体   中英

DataGridView does not rebuild xml file contents in vb.net

I am trying to rebuild table values from an xml file in a DataGridView. First i read the xml document and then load it as a datasource in the DataGridView

Try
    Dim ds As DataSet = New DataSet
    ds.ReadXml("data.xml")
    bs.DataSource = ds.Tables(0)
    GridView.DataSource = bs
Catch ex As Exception
    MessageBox.Show(ex.ToString)
End Try

now on a button click i want to rebuild the data table I have tried different methods but non work

OnButton_Click
    bs.ResetBindings(False) ' Only blinks but does not work '

OnButton_Click
    bs.Clear()
    bs.DataSource = ds.Tables(0) ' Still does not work '

OnButton_Click
    GridView.DataSource = Nothing
    GridView.DataSource = bs

OnButton_Click
    GridView.Refresh()

Can someone point me where i am wrong

---------------------UPDATE-------------------

the xml file is generated and maintained separately rather through edit in the gridview itself

Private Sub addClient(ByRef hwid As String, ByRef dataID As String, ByRef dataValue As String)

    Dim xmlDoc As New XmlDocument()

    xmlDoc.Load(_datapath)

    Dim rootNode As XmlNode = xmlDoc.SelectSingleNode("users")
    Dim userNodes As XmlNodeList = rootNode.SelectNodes("user")
    Dim userNode As XmlNode = rootNode.SelectSingleNode("user")
    Dim Isuid As Boolean = False


    For Each uNode As XmlNode In userNodes
        If IsNothing(uNode.Attributes("uid")) Then
            Dim idAttr As XmlAttribute = xmlDoc.CreateAttribute("uid")
            idAttr.InnerText = hwid
            uNode.Attributes.Append(idAttr)

            Dim idNode As XmlNode = xmlDoc.CreateElement(dataID)
            idNode.InnerText = dataValue
            userNode.AppendChild(idNode)
        Else
            If uNode.Attributes("uid").Value = hwid Then
                ' Alter data '
                Isuid = True
                userNode = uNode
                GoTo end_of_for
            End If
        End If
    Next


end_of_for:
    If Isuid = True Then
        ' Alter the data '
        If IsNothing(userNode.SelectSingleNode(dataID)) Then
            Dim idNode As XmlNode = xmlDoc.CreateElement(dataID)
            idNode.InnerText = dataValue
            userNode.AppendChild(idNode)
        Else
            Dim idNode As XmlNode = userNode.SelectSingleNode(dataID)
            idNode.InnerText = dataValue
            userNode.AppendChild(idNode)
        End If
    Else
        ' Create a new one '
        If rootNode.SelectSingleNode("user").Attributes("uid").Value <> hwid Then
            userNode = xmlDoc.CreateElement("user")
            rootNode.AppendChild(userNode)
            Dim idAttr As XmlAttribute = xmlDoc.CreateAttribute("uid")
            idAttr.InnerText = hwid
            userNode.Attributes.Append(idAttr)
            Dim idNode As XmlNode = xmlDoc.CreateElement(dataID)
            idNode.InnerText = dataValue
            userNode.AppendChild(idNode)
        End If
    End If

do_exit:
    xmlDoc.Save(_datapath)

End Sub

here is my rest of the code

Private _datapath As String = "data.xml"
Dim bs As New BindingSource
Dim ds As New DataSet

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    ' Add user element also '
    If Not File.Exists(_datapath) Then
        Dim xmlDoc As New XmlDocument()
        Dim docNode As XmlNode = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", Nothing)
        xmlDoc.AppendChild(docNode)
        Dim rootNode As XmlNode = xmlDoc.CreateElement("users")
        xmlDoc.AppendChild(rootNode)
        Dim userNode As XmlNode = xmlDoc.CreateElement("user")
        rootNode.AppendChild(userNode)
        xmlDoc.Save(_datapath)
    End If

    Try
        ds.ReadXml(_datapath)
        bs.DataSource = ds.Tables(0)
        ClientView.DataSource = bs
    Catch ex As Exception
        MessageBox.Show(ex.ToString)
    End Try

End Sub

Private Sub btn1_Click(sender As Object, e As EventArgs) Handles btn1.Click
bs.ResetBindings(False)
End Sub

Edit2 : So if I load again the file (button1_Click), it is still working. I noticed it is not working if the xml schema does not match the xml file. If the schema is not ok, I get the same result as you; no data in the grid

Edit : check before rebinding if you don't change in some cases the structure\\schema of the xml. This works for me. Need to read the schema too before reading the xml.

public partial class Form1 : Form
{
    DataSet ds1 = new DataSet();
    BindingSource bs1 = new BindingSource();

    public Form1()
    {
        InitializeComponent();
    }

    //Initial
    private void button1_Click(object sender, EventArgs e)
    {
        DataTable dt1 = new DataTable();

        ds1.ReadXmlSchema("XMLFile1Schema.xml");
        ds1.ReadXml("XMLFile1.xml", XmlReadMode.ReadSchema);

        bs1.DataSource = ds1.Tables[0];
        dataGridView1.DataSource = bs1;
    }

    //Change content
    private void button2_Click(object sender, EventArgs e)
    {
        DataTable dt2 = new DataTable();
        dt2.Columns.Add("Column1");
        dt2.Columns.Add("Column2");

        dt2.Rows.Add("B11", "B12");
        dt2.Rows.Add("B21", "B22");

        dataGridView1.DataSource = dt2;
    }

    //Reset
    private void button3_Click(object sender, EventArgs e)
    {
        //dataGridView1.DataSource = ds1.Tables[0];
        dataGridView1.DataSource = bs1;
    }
}

The xml schema:

<?xml version="1.0" standalone="yes"?>
<xs:schema id="NewDataSet" xmlns=""      xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:element name="NewDataSet" msdata:IsDataSet="true"    msdata:MainDataTable="row" msdata:UseCurrentLocale="true">
  <xs:complexType>
  <xs:sequence minOccurs="0" maxOccurs="unbounded">
    <xs:element name="row">
      <xs:complexType>
        <xs:sequence>
          <xs:element name="col1" type="xs:string" minOccurs="1" />
          <xs:element name="col2" type="xs:string" minOccurs="1" />
        </xs:sequence>
      </xs:complexType>
    </xs:element>
  </xs:sequence>
</xs:complexType>

The xml file:

<?xml version="1.0" encoding="utf-8" ?>
<DocumentElement>
 <row>
   <col1>X11</col1>
   <col2>X12</col2>
 </row>
 <row>
   <col1>X21</col1>
   <col2>X22</col2>
 </row>
</DocumentElement>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM