locked
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"
                _tablaClientes.Columns.Add(columna)
    
                'Razon Social
                columna = New DataColumn
                .DataType = System.Type.GetType("System.Int32")
                .AllowDBNull = False
                .Caption = "Razón social"
                .ColumnName = "RazonSocial"
                _tablaClientes.Columns.Add(columna)
    
                'Direccion
                columna = New DataColumn
                .DataType = System.Type.GetType("System.String")
                .AllowDBNull = False
                .Caption = "Dirrecion"
                .ColumnName = "Direccion"
                _tablaClientes.Columns.Add(columna)

    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
            objetoEstructura.llenarDataTable()
            Try
                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)
    
                    objetoEstructura.tablaClientesP.Rows.Add(fila)
                    objetoEstructura.tablaClientesP.AcceptChanges()
                Next
                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.

    <Empresas>
    <Empresa>
    <CedulaJuridica>3101162637</CedulaJuridica>
    <RazonSocial>kUNG FU PANDA MOVIES S A</RazonSocial>
    <Direccion/>
    <Provincia/>
    <Canton/>
    <Telefono/>
    </Empresa>
    <Empresa>
    </Empresas>

    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
            dt.Columns.Add("CedulaJuridica")
            dt.Columns.Add("RazonSocial")
            dt.Columns.Add("Direccion")
            dt.Columns.Add("Provincia")
            dt.Columns.Add("Canton")
            dt.Columns.Add("Telefono")
    
            Try
                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
                    dt.Rows.Add(fila)
                Next
                dgvClientesFactura.DataSource = dt
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try

    Result:

    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