Using a DataGridView in a WinForms Application in Oxygene for .Net

This series of steps requires the use of Oxygene within Visual Studio.

The result will be a program in which you can enter some data, navigate through the rows, delete rows of data and save the data automatically on closing the form. The following screenshot shows "one we have prepared earlier".

DataGridView showing Goalscorers

DataGridView showing Goalscorers

  1. Select menu item File > New > Project (or type Ctrl+Shift+N), highlight the WinForms Application and name the project Local_XML_File. Click the OK button.

  2. Click on the vertical Toolbox tag on the left of Visual Studio and unfold the data components:
    Data Components

    Data Components

  3. Double click on DataSet and select an untyped dataset from the resulting dialogue.
  4. In the Properties pane at the bottom right of Visual Studio, click on the ellipsis for Tables.
    Data Components

    Data Components

    This opens the Tables Collection Editor.

  5. Click on the Add button to add a table. Change the TableName and Design Name to Goalscorers.
  6. Click on the Columns ellipsis to open the Columns Collection Editor.

  7. Click on the Add button and change the ColumnName property to Forename.

    This should change the Caption property to Forename when you enter the data.

  8. Similarly, add the Surname and Goals columns.

  9. Change the DataType of the Goals column to System.Int32 (to allow for further processing if necessary) and close both dialogues.

  10. Add a DataGridView and a BindingSource by double clicking on their names in the Toolbox.
  11. Set the DataSource property of bindingSource1 to dataSet1.
  12. Set the DataSource property of dataGridView1 to Goalscorers (within bindingSource1).

    The headings Forename and Surname are now visible in the design view of the form.

  13. Drag the right edge of the DataGridView until the Goals column shows.
  14. Click in the form and then on the Events (lightning icon) tab of its properties.
  15. Double click on Load.

    You should now see method MainForm.MainForm_Load in the editor.

  16. Enter the code: if File.Exists('Goalscorers.xml') then dataSet1.ReadXml('Goalscorers.xml');
  17. Add System.IO to the uses clause.
  18. Double click on the FormClosing property and enter into the MainForm.MainForm_FormClosing procedure the code dataSet1.WriteXml('Goalscorers.xml');

    You can now start the program, enter data then close the form. When you rerun the program you should see the data that you entered.

  19. From the Toolbox, add a BindingNavigator to the form and drag down the DataGridView until both controls are fully visible. Set the BindingSource property to goalscorersBindingSource.

    You can now run the program, navigate through the rows of data and delete a row.

  20. Change the Text property of the form to Goalscorers.
  21. Click in the ellipsis for the ColumnHeadersDefaultCellStyle of the DataGridView and unfold the Font property in the CellStyle Builder dialogue. Change the Bold property to True.

    You should now have a form similar to the screenshot at the top of this page.

You should find Goalscorers.xml in the same folder as the executable. You can open it in a text editor and edit it then confirm that the changes show up in the DataGridView next time you run the program.

Programming - a skill for life!

How to write programs in the Oxygene for .Net dialect of Pascal