Using a DataGridView in a WinForms Application in Oxygene for .Net
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
Select menu item Local_XML_File. Click the OK button.
(or type Ctrl+Shift+N), highlight the WinForms Application and name the project
Click on the vertical Toolbox tag on the left of Visual Studio and unfold the data components:
- Double click on DataSet and select an untyped dataset from the resulting dialogue.
In the Properties pane at the bottom right of Visual Studio, click on the ellipsis for Tables.
This opens the Tables Collection Editor.
- Click on the Add button to add a table. Change the TableName and Design Name to Goalscorers.
Click on the Columns ellipsis to open the Columns Collection Editor.
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.
Similarly, add the Surname and Goals columns.
Change the DataType of the Goals column to System.Int32 (to allow for further processing if necessary) and close both dialogues.
- Add a DataGridView and a BindingSource by double clicking on their names in the Toolbox.
- Set the DataSource property of bindingSource1 to dataSet1.
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.
- Drag the right edge of the DataGridView until the Goals column shows.
- Click in the form and then on the Events (lightning icon) tab of its properties.
Double click on Load.
You should now see method MainForm.MainForm_Load in the editor.
- Enter the code: if File.Exists('Goalscorers.xml') then dataSet1.ReadXml('Goalscorers.xml');
- Add System.IO to the uses clause.
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.
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.
- Change the Text property of the form to Goalscorers.
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.