DataGridView
This control displays data from SQL databases. This tutorial takes a specific table from a database (as a DataTable
) and displays it on a DataGridView
.
In Visual Studio, select the File menu and then New Project, and select a Windows Forms application. Next drag the DataGridView
icon into the window.
Here we need to generate an SDF file to interact with. We will use Visual Studio for this part. Go to Data and then Add New Data Source.
Data Source=C:\Users\Sam\Documents\Data.sdf
Next, we need to have actual data in the table we created. Return to the Server Explorer, and right-click on the table, such as the Animals table. Type in 10 for Weight, and brown for Color.
Weight: 10 Color: BrownWeight: 15 Color: BlackWeight: 5 Color: GreenWeight: 20 Color: White
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.
FillData
after InitializeComponent
. In FillData
we put the database contents into the DataGridView
.Properties.Settings
Default.DataConnectionString
, which was generated.DataAdapter
: SqlCeDataAdapter
. A DataAdapter
specifies a command that directly fills a DataSet
or DataTable
.DataSource
in the DataGridView
, which renders the contents of the database onto the screen.using System.Data; using System.Data.SqlServerCe; using System.Windows.Forms; namespace WindowsFormsApplication5 { public partial class Form1 : Form { public Form1() { InitializeComponent(); FillData(); } void FillData() { // Step 1: Open connection. using (SqlCeConnection c = new SqlCeConnection( Properties.Settings.Default.DataConnectionString)) { c.Open(); // 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(); a.Fill(t); // Step 4: Render data onto the screen. dataGridView1.DataSource = t; } } } } }
For usability, you want to have alternating row colors on your DataGridView
. The .NET Framework provides the AlternatingRowsDefaultCellStyle
attribute.
BackColor
to something your users will enjoy, and that will improve the program's usability. I chose aqua.DataSet
The DataSet
object available in System.Data
has a slightly different purpose from DataTable
, as it allows more information to be stored.
The default DataGridView
appearance and functionality is not usually the best. Here we make some tweaks to the DataGridView
to make it more pleasing.
DataGridView
in the designer view to "pin" it to each edge of your window.DataGridView
white by changing the Appearance and then BackgroundColor
from AppWorkspace
to Window.RowHeadersVisible
and change it to False.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.
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.
DataPropertyName
of the column to the column name from your database.DataGridViewColumn
be used for the Weight column for your database, type "Weight" into the DataPropertyName
box.We used a database and displayed its contents in a usable DataGridView
. This is critical for many data-driven Windows Forms application.