Wednesday, May 13, 2020

[VB][Example] Bind xml file to DataGridView

Step 1) Prepare xml file
Here I name it as products.xml.
<?xml version="1.0"?>
<products>
  <product>
    <name>Waterman</name>
    <price>10</price>
  </product>
  <product>
    <name>Banana</name>
    <price>2</price>
  </product>
  <product>
    <name>Cheery</name>
    <price>0.2</price>
  </product>
  <product>
    <name>Apple</name>
    <price>3</price>
  </product>
</products>


Step 2) Prepare DataGridView
Create a DataGridView in user-interface, here I name it as "DataGridViewContent"
Select "Common controls"->"Data"->"DataGridView" in Toolbox, Drag it into interface

Step 3) Visual Basic part
Dim dataSet As New DataSet
dataSet.ReadXml("products.xml")
DataGridViewContent.DataSource = dataSet.Tables(0)

No comments :

Post a Comment