User Tools

Site Tools


17.7.6 GridView - Data export to a csv file

This project is not primarily concerned with displaying data in a GridView, but with backing up data that is displayed in a GridView. The data is saved in a csv file.

This text file format is used as a transfer format for simply structured data. Each row contains a data record, where the individual data fields are separated from the other fields by a field separator and a field value is enclosed by a text separator. Optionally, the first line can also contain the field labels.


Figure Saving data in a csv file

Here you see a section with the data stored in a csv file - please compare with figure - where the 4 field names are stored in the first data set:


The comma serves as field separator and the quotation mark '' is used as text separator. In the export form, you can select both the field separator and the text separator from a predefined character set. You can also specify whether the field names are to be saved in the first data record.


Figure Specifying storage options in a separate form

You determine the storage location and the file name for the csv file in a dialog:


Figure save dialog

To use the source code for saving data in a csv file in other projects, a separate form for setting the saving options and a module were used.

The complete source code for the module can be found here:

' Gambas module file
PUBLIC sFeldTrennzeichen AS String
PUBLIC sTextTrennzeichen AS String
PUBLIC bSaveTitel AS Boolean
PUBLIC bError AS Boolean
PUBLIC SUB ExportGridView2CSV(Grid AS GridView,pFeldTrenner AS String, pTextTrenner AS String, /
                              pSaveTitel AS Boolean)
  DIM iCount, rCount, cCount AS Integer
  DIM sZeile AS String
  DIM hFile AS File
  Dialog.Title = "Speichern Sie die Daten in einer csv-Datei!"
  Dialog.Filter = ["*.csv", "csv-Dateien"]
  IF Dialog.SaveFile() = TRUE THEN 
     Message.Warning("Der Daten-Export wurde abgebrochen!")
     RETURN ' Cancel-Button     
   ' Die CSV-Datei wird neu angelegt oder geleert
     IF File.Ext(Dialog.Path) <> "csv" AND File.Ext(Dialog.Path) <> "txt" THEN 
        Dialog.Path = File.SetExt(Dialog.Path, "csv")
     hFile = OPEN Dialog.Path FOR WRITE CREATE
        Message.Error(Error.Text & Error.Where)
     seek">SEEK #hFile, 0
   ' Spaltenüberschriften speichern (optional)
     IF pSaveTitel = TRUE THEN 
        IF Grid.Header = Grid.Horizontal OR Grid.Header = Grid.Both THEN
           FOR iCount = 0 TO Grid.Columns.Count - 1
               IF iCount > 0 THEN 
                  sZeile &= pFeldTrenner & pTextTrenner & Grid.Columns[iCount].Title & pTextTrenner
                  sZeile &= pTextTrenner & Grid.Columns[iCount].Title & pTextTrenner
               ENDIF ' iCount > 0
           PRINT #hFile, sZeile 
        ENDIF ' Grid.Horizontal OR Grid.Header?
     ENDIF ' bTitel = TRUE?
   ' GridView-Daten speichern
     sZeile = ""
     FOR rCount = 0 TO Grid.Rows.Count - 1
         FOR cCount = 0 TO Grid.Columns.Count - 1
             IF cCount > 0 THEN 
                sZeile &= pFeldTrenner & pTextTrenner & Grid[rCount, cCount].Text & pTextTrenner
                sZeile &= pTextTrenner & Grid[rCount, cCount].Text & pTextTrenner
             ENDIF ' cCount > 0  
         NEXT ' cCount
         PRINT #hFile, sZeile 
         sZeile = ""
     NEXT ' rCount  
   ENDIF ' Dialog.SaveFile() = TRUE?
  CLOSE #hFile
END ' GridViewExport2CSV

In the main program, the procedure with which a data export is started is of particular interest:

PUBLIC SUB ExportGridView2CSV()
     MCSV.GridView2CSV(GridView1, MCSV.sFeldTrennzeichen, MCSV.sTextTrennzeichen, MCSV.bSaveTitel)
     btnGridViewExportCSV.Enabled = TRUE
    Message.Warning("Der Daten-Export wurde abgebrochen!")
    btnGridViewExportCSV.Enabled = TRUE
  ENDIF ' MCSV.bError = FALSE?
END ' ExportGridView2CSV()


The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k17/k17.7/k17.7.6/start.txt · Last modified: 30.09.2023 by emma

Page Tools