I need some way to save and read a file using a DataGridView.
Databases are out of the question, because I'm going for something template-style.
And the user will probably make lots of templates.

I tried saving/reading from/into XML, but I got lost.

Thanks.

If your DataGridView has a datatable as its datasource why not make use of the datatable read and write xml methods

You could use a simple .csv style, with the first line containing the column headers. That becomes the template for the data in the file.

I'm trying to save it as XML again, heres my code:

Private Sub Save_Click(sender As Object, e As EventArgs) Handles Save.Click

        If SaveFile.ShowDialog() = DialogResult.OK Then
            dt.TableName = "TimedMessengerTemplate"
            dt.WriteXml(SaveFile.FileName)
        End If
    End Sub

and that outputs:

<?xml version="1.0" standalone="yes"?>
<DocumentElement>
  <TimedMessengerTemplate />
  <TimedMessengerTemplate />
  <TimedMessengerTemplate />
  <TimedMessengerTemplate />
</DocumentElement>

which does not contain any data, obviously.

If the table doesn't contain any data, then the .xml file won't either. It appears your table has 4 empty rows

I filled all of the rows with something.

Probably important:

 Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        DataGrid.DataSource = dt
    End Sub

There must be something in your code somewhere that is clearing the data fron the rows. The rows being added without adding any data to them will result in the xml that you've shown. The code you've shown works right for me.

I'm wondering if something like this is what you are after:

dt.WriteXmlSchema(Path)

This produces something like this:

<?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="tblPhonEntries" msdata:UseCurrentLocale="true">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="tblPhonEntries">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Column1" type="xs:string" minOccurs="0" />
              <xs:element name="Column2" type="xs:string" minOccurs="0" />
              <xs:element name="Column3" type="xs:string" minOccurs="0" />
              <xs:element name="Column4" type="xs:string" minOccurs="0" />
              <xs:element name="Column5" type="xs:string" minOccurs="0" />
              <xs:element name="Column6" type="xs:string" minOccurs="0" />
              <xs:element name="Column7" type="xs:string" minOccurs="0" />
              <xs:element name="Column8" type="xs:string" minOccurs="0" />
              <xs:element name="Column9" type="xs:string" minOccurs="0" />
              <xs:element name="Column10" type="xs:string" minOccurs="0" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>

When read into a DataTable(dt.ReadXmlSchema(Path)) this produces all the columns but no rows. Allowing you to add the data you need.

See this example :

This is the xml file named student.xml :

<?xml version="1.0" standalone="yes"?>
<Table>
  <Students>
    <No>1</No>
    <Name>Jery</Name>
    <Address>New York</Address>
  </Students>
  <Students>
    <No>2</No>
    <Name>Dewi</Name>
    <Address>Olympus</Address>
  </Students>
  <Students>
    <No>3</No>
    <Name>Me</Name>
    <Address>earth</Address>
  </Students>
  <Students>
    <No>4</No>
    <Name>Craig</Name>
    <Address>Jogja</Address>
  </Students>
</Table>

This how to read and write it through datagridview :
Just add datagridview and button.

Imports System.Xml

Public Class Form1
    Dim ds As New DataSet
    Dim bs As New BindingSource
    Dim xFile As String = "student.xml"
    Dim flag As Boolean = False

    Private Sub ReadXML()
        ds.ReadXml(xFile)
        bs.DataSource = ds.Tables(0)
        DataGridView1.DataSource = bs
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If flag Then
            ds.WriteXml(xFile)
            flag = False
        End If
    End Sub

    Private Sub DataGridView1_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
        Flag = True
        bs.ResetBindings(False)
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ReadXML()
    End Sub
End Class

The button does not appear to do anything.

tinstaafl, I'd like to save all of the information.
Probably not the columns, because they are going to stay the same.

Perhaps you need to re-explain what you need. You originally mentioned templates, and templates generally don't have much data. They are usually just shells, that show the format, ready to put data in.

I'd like to save all of the information in the rows.
I probably should of said "save" or "saved data"...

I think you'll have to show how you populate the DataTable

The code you've used to save the xml will work according to the data in the table. So somehow the data isn't what you think it is.

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.