Results 1 to 6 of 6

Thread: Using CSV file format when saving/opening with DataGridView

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    95

    Using CSV file format when saving/opening with DataGridView

    Hello, I am developing a piece of software that utilizes the DataGridView feature in Visual Studio. I do not set any of the columns up in code but in the designer. I have never done this before and no idea where to begin with how to save the data in the correct order, correct order in columns and rows it was in before, and have it open that file and have it all in the correct order. I was wondering if anyone could explain this to me. Thanks if you can!

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Using CSV file format when saving/opening with DataGridView

    Ok, so what is the CSV part about?

    The reason I ask is that there is a much easier way to do this if you aren't tied to CSV. Datatables have WriteXML/ReadXML methods, which will create a text file, but it will be XML format rather than CSCV. A datatable can also be the datasource for a DataGridView. In fact, the datatable would be the easiest way to hold data shown in a DGV.

    So, to put it all together, you could create a datatable and add columns to it. This datatable would then be set as the datasource for the DGV, such that any records added/edited/deleted in the DGV would actually be recorded in the datatable. Setting the datatable as the datasource of the DGV would take one line. The datatable could then be saved/loaded to/from a text file (in XML format) with only a single line.

    It doesn't get easier than that, but then again, that description doesn't perfectly match what you described, which is why I asked about the CSV. If you can flex a bit, you can get close to what you want with great ease, but if you do require CSV, then it gets a bit more complicated.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    95

    Re: Using CSV file format when saving/opening with DataGridView

    I was thinking about using XML but I do not know why I didn't want to. Yeah XML should work fine but the issue is, I do not know how. Perhaps know how to or know of a youtube video that explains it well?

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Using CSV file format when saving/opening with DataGridView

    If you are using a datatable, then all you need to do to turn it into XML is to call the WriteXML method. That method takes a file name (full path plus file name) as the only argument. In other words, you don't need to know anything at all about XML for that, all you need to do is say what file to save it to, and the rest is done for you. Any file saved in that fashion can also be read back into a datatable with the ReadXML method, which takes the same argument.

    Of course, there are much more complicated ways to write XML. I don't tend to work with XML in those more complicated ways, but that's largely because there isn't so much need to do that, except with XML Serialization, which is a different way to write objects to XML.

    So, there isn't much to show, because with datatables, it's just too simple for much of a tutorial to be written. The datatable itself might be a question, though that's not terribly difficult, either. At worst, you just create a New datatable, then add columns to the Columns collection of the datatable. Doing that last step can get tricky, but the easiest way is to do something like this:

    MyTable.Columns.Add(New Column("ColumnNameHere",DataTypeForColumnHere))

    If you are reading the datatable in from an XML file, you don't need to do that. When reading the data in from XML, you just need to create the datatable. Adding the columns and getting the types right is done for you when you read in the XML.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    95

    Re: Using CSV file format when saving/opening with DataGridView

    Sorry for the late reply. Question on the "MyTable.Columns.Add(New Column("ColumnNameHere",DataTypeForColumnHere))" part. I already have the columns named in the designer but do I still need to do that part as well?

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Using CSV file format when saving/opening with DataGridView

    Quote Originally Posted by Dragnorian View Post
    Sorry for the late reply. Question on the "MyTable.Columns.Add(New Column("ColumnNameHere",DataTypeForColumnHere))" part. I already have the columns named in the designer but do I still need to do that part as well?
    You already have the grid columns, not the table columns.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width