none
Data table, datagrid view, and text file c# help please RRS feed

  • Question

  • I have created a hotel reservation program that has to load and save to a text file, that uses 2 datagridviews and two listviews, the program runs perfectly, all i need to do is implement the saving and the loading, i decided to load all four sepratly, i know how to load listview my only problem is the loading and saving a datagridview to a text file if someone could give me a step by step guide that would great
    Friday, July 11, 2014 8:30 PM

Answers

  • Hello,

    Ideas are provided for how to proceed, no step-by-step direction as this is not the intent of these forums to be for providing step-by-step instructions. Best to consider what is presented, try or not try it, that's up to you.

    First off this is a VB.NET forum thus what I present is VB.NET and if you need C# the methods are the same only the syntax changes

    I would suggest using XML data source instead of text files as you can have a relational xml file but not a relational text file. We can load say customer and customer order and order detail data via DataSet.ReadXml then access the data via DataSet.Table(x) where x is the ordinal index of the table or x can be the table name.

    Simple example of loading and saving data (there is a good deal I do not show that would be needed, only concentrating on loading and saving)

    Public Class Form1
        Private FileName As String = IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "CustomersData.xml")
        Private Sub Form2_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
            Dim ds As New DataSet
            ds.Tables.Add(CType(DataGridView1.DataSource, DataTable).Copy)
            ds.DataSetName = "Customers"
            ds.WriteXml(FileName)
        End Sub
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim ds As New DataSet
            ds.ReadXml(FileName)
            DataGridView1.DataSource = ds.Tables("Customer")
        End Sub
    End Class

    Part of the above done in C#

    private string FileName = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "CustomersData.xml");

    .

    DataSet ds = new DataSet();
    ds.Tables.Add(((DataTable)DataGridView1.DataSource).Copy());
    ds.DataSetName = "Customers";
    ds.WriteXml(FileName);
    

    Past that there is assertion in tangent with add, editing, deleting etc. and there should be a primary key, in your code have you considered a primary key and how to increment it?

    For text files look at TextFieldParser class for reading data then to write data back to the file we can get DataGridView data via a language extension method

    <System.Diagnostics.DebuggerStepThrough()> _
    <Runtime.CompilerServices.Extension()> _
    Public Function ExportRows(ByVal sender As DataGridView) As String()
        Return _
            (
                From row In sender.Rows
                Where Not DirectCast(row, DataGridViewRow).IsNewRow
                Let RowItem = String.Join(",", Array.ConvertAll(DirectCast(row, DataGridViewRow) _
                                        .Cells.Cast(Of DataGridViewCell).ToArray, Function(c As DataGridViewCell) _
                                        If(c.Value Is Nothing, "", c.Value.ToString))) Select RowItem
           ).ToArray
    End Function
    

    To save data returned from the code above use IO.File.WriteAlllines.

    With all that said I would highly suggest use SQL-Server Express database which will make your project 10x better in that it will be easy to control reading and writing data which includes managing relations between tables and finally more secure. Whenever possible databases should be used over xml and delimited files. Now with that mentioned you may very well not want or can't go this route but I need to mention it anyways.

    As you can see I am only providing direction, no step-by-step instructions.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Friday, July 11, 2014 11:02 PM
    Moderator
  • I would make one change to Kevin's code.  You should always save an XML file with the schema to make sure when you read the XML you get the same results that you had before you saved the data.

    ds.WriteXml(FileName, XmlWriteMode.WriteSchema)


    jdweng

    Saturday, July 12, 2014 12:11 AM

All replies

  • Hello,

    Ideas are provided for how to proceed, no step-by-step direction as this is not the intent of these forums to be for providing step-by-step instructions. Best to consider what is presented, try or not try it, that's up to you.

    First off this is a VB.NET forum thus what I present is VB.NET and if you need C# the methods are the same only the syntax changes

    I would suggest using XML data source instead of text files as you can have a relational xml file but not a relational text file. We can load say customer and customer order and order detail data via DataSet.ReadXml then access the data via DataSet.Table(x) where x is the ordinal index of the table or x can be the table name.

    Simple example of loading and saving data (there is a good deal I do not show that would be needed, only concentrating on loading and saving)

    Public Class Form1
        Private FileName As String = IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "CustomersData.xml")
        Private Sub Form2_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
            Dim ds As New DataSet
            ds.Tables.Add(CType(DataGridView1.DataSource, DataTable).Copy)
            ds.DataSetName = "Customers"
            ds.WriteXml(FileName)
        End Sub
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim ds As New DataSet
            ds.ReadXml(FileName)
            DataGridView1.DataSource = ds.Tables("Customer")
        End Sub
    End Class

    Part of the above done in C#

    private string FileName = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "CustomersData.xml");

    .

    DataSet ds = new DataSet();
    ds.Tables.Add(((DataTable)DataGridView1.DataSource).Copy());
    ds.DataSetName = "Customers";
    ds.WriteXml(FileName);
    

    Past that there is assertion in tangent with add, editing, deleting etc. and there should be a primary key, in your code have you considered a primary key and how to increment it?

    For text files look at TextFieldParser class for reading data then to write data back to the file we can get DataGridView data via a language extension method

    <System.Diagnostics.DebuggerStepThrough()> _
    <Runtime.CompilerServices.Extension()> _
    Public Function ExportRows(ByVal sender As DataGridView) As String()
        Return _
            (
                From row In sender.Rows
                Where Not DirectCast(row, DataGridViewRow).IsNewRow
                Let RowItem = String.Join(",", Array.ConvertAll(DirectCast(row, DataGridViewRow) _
                                        .Cells.Cast(Of DataGridViewCell).ToArray, Function(c As DataGridViewCell) _
                                        If(c.Value Is Nothing, "", c.Value.ToString))) Select RowItem
           ).ToArray
    End Function
    

    To save data returned from the code above use IO.File.WriteAlllines.

    With all that said I would highly suggest use SQL-Server Express database which will make your project 10x better in that it will be easy to control reading and writing data which includes managing relations between tables and finally more secure. Whenever possible databases should be used over xml and delimited files. Now with that mentioned you may very well not want or can't go this route but I need to mention it anyways.

    As you can see I am only providing direction, no step-by-step instructions.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Friday, July 11, 2014 11:02 PM
    Moderator
  • I would make one change to Kevin's code.  You should always save an XML file with the schema to make sure when you read the XML you get the same results that you had before you saved the data.

    ds.WriteXml(FileName, XmlWriteMode.WriteSchema)


    jdweng

    Saturday, July 12, 2014 12:11 AM