Using an Access Database with GanttChartDataGrid in a WinForms Application
When building project management applications with DlhSoft Gantt Chart Library for WPF, you sometimes need to integrate existing data sources such as Microsoft Access databases. In this article, we’ll show how you can load and edit tasks stored in an Access .accdb file, while hosting a GanttChartDataGrid control inside a WinForms application.
1. Project Setup
We’ll create a WinForms application that hosts a WPF UserControl (with GanttChartDataGrid).
The structure is simple:
- Form1.cs → WinForms main form, using
ElementHostto display WPF content. - UserControl1.xaml → WPF UI, containing a
GanttChartDataGrid. - UserControl1.xaml.cs → logic to load/save tasks from the Access database.
- Database1.accdb (in AppData) → Access database with a
Taskstable (data shown in the Gantt chart).
2. Hosting WPF in WinForms
In the main form, we just place an ElementHost control, and set its child to our WPF user control:
public Form1()
{
InitializeComponent();
elementHost1.Child = new UserControl1();
}This way, the Gantt chart will render seamlessly inside a WinForms window.
3. Loading Tasks from Access
Our Access database (Database1.accdb) contains a table Tasks with fields:
- Id (primary key);
- Task (string);
- StartDate (DateTime);
- EndDate (DateTime).
Inside UserControl1.xaml.cs, we connect using OleDb:
string connectionString =
@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=.\AppData\Database1.accdb;Persist Security Info=True;";
string query = "SELECT * FROM Tasks";
using (OleDbConnection conn = new OleDbConnection(connectionString))
{
OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn);
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);
var items = new ObservableCollection<GanttChartItem>();
foreach (DataRowView row in dataTable.DefaultView)
{
var item = new GanttChartItem
{
Content = row["Task"],
Start = (DateTime)row["StartDate"],
Finish = (DateTime)row["EndDate"],
Tag = (int)row["Id"]
};
items.Add(item);
}
GanttChartDataGrid.Items = items;
}Each record becomes a GanttChartItem, with the task name, start, and finish dates.
4. Updating the Database
We also want changes made by the user in the GanttChartDataGrid to update the Access database.
Get DlhSoft’s stories in your inbox
Join Medium for free to get updates from this writer.
This is handled in the ItemPropertyChanged event:
private void GanttChartDataGrid_ItemPropertyChanged(
object sender, PropertyChangedEventArgs e)
{
var item = sender as GanttChartItem;
var id = (int)item.Tag;
if (e.PropertyName == "Content" ||
e.PropertyName == "Start" ||
e.PropertyName == "Finish")
{
using (OleDbConnection conn = new OleDbConnection(connectionString))
{
OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM Tasks", conn);
OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter);
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);
var row = dataTable.Select("Id=" + id).Single();
row.BeginEdit();
switch (e.PropertyName)
{
case "Content": row["Task"] = item.Content; break;
case "Start": row["StartDate"] = item.Start; break;
case "Finish": row["EndDate"] = item.Finish; break;
}
row.EndEdit();
adapter.Update(dataTable);
}
}
}This ensures that any change to the task name or dates in the UI is persisted back to Access.
5. Running the Application
When you run the app:
- The GanttChartDataGrid will load tasks directly from your Access database.
- Editing a task’s content or dates will automatically update the database.
This gives you a smooth integration between DlhSoft Gantt Chart Library for WPF and legacy Access-based data sources, even inside a WinForms environment.
Sample app
The full sample source code — either C# or Visual Basic — is available here. Enjoy!









