C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
And: The Toolbox contains the icons and controls you can add. Select View and then Toolbox to show it.
Then: Select Database, and click Next. Click on New Connection, as we need to create an all-new connection and database.
Note: You will get the connection string at this point. The one given to me is as follows.
Next: You will see the "Save the Connection String to the Application Configure File" dialog. Save it as DataConnectionString.
Connection string
Data Source=C:\Users\Sam\Documents\Data.sdf
Note: This data is for the example, but your program may have similar fields. We could be working on a table for a veterinarian's office.
Animal 1
Weight: 10
Color: Brown
Animal 2
Weight: 15
Color: Black
Animal 3
Weight: 5
Color: Green
Animal 4
Weight: 20
Color: White
Using directive: C#
using System.Data.SqlServerCe;
FillData: 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 autogenerated.
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.
C# program that uses SqlCeConnection
using System.Data;
using System.Data.SqlServerCe;
using System.Windows.Forms;
namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
FillData();
}
void FillData()
{
// 1
// Open connection
using (SqlCeConnection c = new SqlCeConnection(
Properties.Settings.Default.DataConnectionString))
{
c.Open();
// 2
// Create new DataAdapter
using (SqlCeDataAdapter a = new SqlCeDataAdapter(
"SELECT * FROM Animals", c))
{
// 3
// Use DataAdapter to fill DataTable
DataTable t = new DataTable();
a.Fill(t);
// 4
// Render data onto the screen
dataGridView1.DataSource = t;
}
}
}
}
}
Also: Hide the row headers. Go to Appearance and then RowHeadersVisible and change it to False.
Tutorial progress: What we have so far is a single window with a DataGridView that expands, rows that are selected the entire way across.
And: The data from our database is now being fully displayed from a DataAdapter.
Note: For the tutorial, we want the Animal table's Weight column to be 110px wide.
Tip: Sometimes it is easier to directly use the DataSource property on the DataGridView.
Note: For the example, I change the Weight cell header to "Weight". This ensures users will know to use pounds, not kilograms.
Finally: Change the properties of the DataGridViewColumn in the dialog box. I set 110px as the width and AutoSizeMode of None.
So: To specify that your DataGridViewColumn be used for the Weight column for your database, type "Weight" into the DataPropertyName box.
Tip: Change the BackColor to something your users will enjoy, and that will improve the program's usability. I chose aqua.
DataGridView ColorsFixedSingle: This border looks better in Windows XP. The None option might be good for some programs.