Adding directive. Before you add logic, add the SqlCeServer directive at the top of your Windows Forms program's code. This enables easier access to important types.
Data adapter. In C# code, you must open a connection to your database first, and then create a DataAdapter. There are different adapter implementations, but they all work in similar ways.
Detail The example calls FillData after InitializeComponent. In FillData we put the database contents into the DataGridView.
Step 1 It opens a connection to the database. We use Properties.Settings Default.DataConnectionString, which was generated.
Step 2 It uses a new DataAdapter: SqlCeDataAdapter. A DataAdapter specifies a command that directly fills a DataSet or DataTable.
Step 3 It assigns the DataSource in the DataGridView, which renders the contents of the database onto the screen.
public partial class Form1 : Form
// Step 1: Open connection.
using (SqlCeConnection c = new SqlCeConnection( Properties.Settings.Default.DataConnectionString))
// Step 2: Create new DataAdapter.
using (SqlCeDataAdapter a = new SqlCeDataAdapter("SELECT * FROM Animals", c))
// Step 3: Use DataAdapter to fill DataTable.
DataTable t = new DataTable();
// Step 4: Render data onto the screen.
dataGridView1.DataSource = t;
DataSet. The DataSet object available in System.Data has a slightly different purpose from DataTable, as it allows more information to be stored.
Improvements. The default DataGridView appearance and functionality is not usually the best. Here we make some tweaks to the DataGridView to make it more pleasing.
Detail Use the Anchor property on the DataGridView in the designer view to "pin" it to each edge of your window.
Detail Make the DataGridView white by changing the Appearance and then BackgroundColor from AppWorkspace to Window.
Detail Hide the row headers. Go to Appearance and then RowHeadersVisible and change it to False.
Add columns. With the DataGridView, you can predefine columns for display. This doesn't display anything. You create the rules by which the actual data will be displayed.
Step 1 Go to Properties and then locate Columns. This shows the Edit Columns dialog box—click on the "Add" button.
Step 2 Enter Header text. The text you type into the Header text box will ensure that the text is always shown.
Step 3 Change the properties of the DataGridViewColumn in the dialog box. I set 110 pixels as the width and AutoSizeMode of None.
DataPropertyName. You need to specify that a certain column in your database be inserted into the Column you just added in the Columns dialog box.
Tip To do this, you must assign the DataPropertyName of the column to the column name from your database.
So To specify that your DataGridViewColumn be used for the Weight column for your database, type "Weight" into the DataPropertyName box.
Alternating colors. For usability, you want to have alternating row colors on your DataGridView. The .NET Framework provides the AlternatingRowsDefaultCellStyle attribute.
Tip Change the BackColor to something your users will enjoy, and that will improve the program's usability. I chose aqua.
A summary. We used a database and displayed its contents in a usable DataGridView. This is critical for many data-driven Windows Forms application.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Oct 4, 2022 (edit link).