Loop through dataTable to store it in datagridview RRS feed

  • Question

  • Good evening, I already have my DataTable structured as follows

                .DataType = System.Type.GetType("System.String")
                .AllowDBNull = False
                .Caption = "Cédula jurídica"
                .ColumnName = "CedulaJuridica"
                'Razon Social
                columna = New DataColumn
                .DataType = System.Type.GetType("System.Int32")
                .AllowDBNull = False
                .Caption = "Razón social"
                .ColumnName = "RazonSocial"
                columna = New DataColumn
                .DataType = System.Type.GetType("System.String")
                .AllowDBNull = False
                .Caption = "Dirrecion"
                .ColumnName = "Direccion"

    Now as I go through it so that I can go through it, and at the end show it in a datagridview can you help me please. I have the following I am new to programming, but I try to do my best.

        Public Sub llenarCliente()
            Dim archivoXML As XmlDocument
            Dim nodeLista As XmlNodeList
            Dim node As XmlNode
            Dim arregloClientes() As String
                archivoXML = New XmlDocument
                archivoXML.Load(Application.StartupPath & "\Empresas.xml")
                nodeLista = archivoXML.SelectNodes("/Empresas/Empresa")
                Dim fila As DataRow
                For Each node In nodeLista
                    fila = objetoEstructura.tablaClientesP.NewRow
                    fila("CedulaJuridica") = arregloClientes(0)
                    fila("RazonSocial") = arregloClientes(1)
                    fila("Direccion") = arregloClientes(2)
                    fila("Provincia") = arregloClientes(3)
                    fila("Canton") = arregloClientes(4)
                    fila("Telefono") = arregloClientes(5)
                dgvClientesFactura.DataSource = objetoEstructura.tablaClientesP
            Catch ex As Exception
            End Try
        End Sub

    Wednesday, July 22, 2020 4:50 AM

All replies

  • You probably must compute arregloClientes. Show a sample part of Empresas.xml.

    Wednesday, July 22, 2020 5:42 AM
  • This is my xml file, it is something extensive, I will only put a part.

    <RazonSocial>kUNG FU PANDA MOVIES S A</RazonSocial>

    Wednesday, July 22, 2020 5:46 AM
  • Try something like this:

       fila("CedulaJuridica") = node.SelectSingleNode("CedulaJuridica").InnerText

       fila("RazonSocial") = node.SelectSingleNode("RazonSocial").InnerText

    Also add MsgBox(ex.Message) inside Catch block to show the errors.

    Although, there are more techniques to read XML data.

    Wednesday, July 22, 2020 5:57 AM
  • Hi dianas28,

    You can also use 'XDocument' to read XML data.

    Here's an example you can refer to:

            Dim dt As DataTable = New DataTable
                Dim fila As DataRow
                Dim xdoc As XDocument = XDocument.Load("your xml path")
                For Each xele As XElement In xdoc.Root.Elements
                    fila = dt.NewRow
                    fila("CedulaJuridica") = xele.Element("CedulaJuridica").Value
                    fila("RazonSocial") = xele.Element("RazonSocial").Value
                    fila("Direccion") = xele.Element("Direccion").Value
                    fila("Provincia") = xele.Element("Provincia").Value
                    fila("Canton") = xele.Element("Canton").Value
                    fila("Telefono") = xele.Element("Telefono").Value
                dgvClientesFactura.DataSource = dt
            Catch ex As Exception
            End Try


    Hope it could be helpful.

    Best Regards,

    Xingyu Zhao

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, July 23, 2020 2:32 AM