I am trying to read the following xml file into my datagridview

<?xml version="1.0" encoding="utf-8" ?>
<DEFLECTIONS_TABLE>
  <Deflections number="1">

    <Service_Class>A</Service_Class>

    <Verticle>600</Verticle>

    <Horizontal>400</Horizontal>

    <Service_Class>B</Service_Class>

    <Verticle>600</Verticle>

    <Horizontal>400</Horizontal>

    <Service_Class>C</Service_Class>

   <Verticle>600</Verticle>

   <Horizontal>400</Horizontal>

   <Service_Class>D</Service_Class>

    <Verticle>800</Verticle>

    <Horizontal>400</Horizontal>

   <Service_Class>E</Service_Class>

   <Verticle>1000</Verticle>

   <Horizontal>400</Horizontal>

    <Service_Class>F</Service_Class>

    <Verticle>1000</Verticle>

     <Horizontal>400</Horizontal>

    </Deflections>

</DEFLECTIONS_TABLE>

I made my windowsform and put a datagridview inside called deflection_datagrid.

Dim filepath As String = "C:\NPF\NBS Design\Crane Beam\Main\Crane Design V1\Crane Design V 1.0\XMLFile1.xml"
        Dim deflection_dataset As New DataSet

        Try

            deflection_dataset.ReadXml(filepath)

            Deflections_datagrid.DataSource = deflection_dataset

            Deflections_datagrid.DataMember = "Deflections"
        Catch

            MessageBox.Show(Err.Description)

        End Try

I've tried to use this coding, but it will not load into the datagridview nor does it give me any error.

Any suggestions?????

Thanks

Recommended Answers

All 4 Replies

One thing I see, your xml file when read into a dataset creates 4 data tables. I'm not sure what you want displayed, but if you want all the data from the xml file displayed in the datagridview you'll have to change the structure of your xml.

Sorry, I'm new to using xml. I made some changes that I though would help from reading some things online, but it still will not load nor will an error pop up. Here's my new xml file

<?xml version="1.0" encoding="utf-8"?>
<TABLE>
   <Deflections>
    <Service_Class>A</Service_Class>

    <Verticle>600</Verticle>

    <Horizontal>400</Horizontal>

  </Deflections>

  <Deflections>

    <Service_Class>B</Service_Class>

    <Verticle>600</Verticle>

    <Horizontal>400</Horizontal>

  </Deflections>

   <Deflections>

       <Service_Class>C</Service_Class>

       <Verticle>600</Verticle>

      <Horizontal>400</Horizontal>

  </Deflections>

    <Deflections>

       <Service_Class>D</Service_Class>

        <Verticle>800</Verticle>

       <Horizontal>400</Horizontal>

    </Deflections>

      <Deflections>

       <Service_Class>E</Service_Class>

      <Verticle>1000</Verticle>

       <Horizontal>400</Horizontal>

     </Deflections>

     <Deflections>

       <Service_Class>F</Service_Class>

        <Verticle>1000</Verticle>

         <Horizontal>400</Horizontal>

      </Deflections>


</TABLE>

I would like my datagridview to read the parent Deflections so that the user will be able to view and possible make changes.

Your updated xmlwill only have 1 table now if that is what you are desiring. I ran the code and it displays the way I understand you want. I added a little updating code to it

Public Class Form1
    Dim bs As New BindingSource
    Dim editFlag As Boolean = False
    Dim filepath As String = "text.xml"
    Dim deflection_dataset As New DataSet

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            deflection_dataset.ReadXml(filepath)
            bs.DataSource = deflection_dataset.Tables(0)
            Deflections_datagrid.DataSource = bs
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub

    Private Sub btnSaveChanges_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveChanges.Click
        If editFlag Then
            Try
                deflection_dataset.WriteXml("Text.xml")
                editFlag = False
                MsgBox("Changes to the XML have been SAVED", MsgBoxStyle.Information, "SUCCESS")
            Catch ex As Exception
                MsgBox("CHANGES HAVE NOT BEEN SAVED", MsgBoxStyle.Critical, "FAILURE")
            End Try

        End If

    End Sub


    Private Sub Deflections_datagrid_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Deflections_datagrid.CellEndEdit
        editFlag = True
        bs.ResetBindings(False)

    End Sub
End Class

That worked Perfect!! Thats Exactly what I was looking for.

Thanks kRod!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.