Skip to content

Commit b820237

Browse files
ES-1025386 Resolved the ReadMe Length Issues in Public Syncfusion Code Examples
1 parent e881797 commit b820237

1 file changed

Lines changed: 133 additions & 1 deletion

File tree

README.md

Lines changed: 133 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,134 @@
11
# Bind SQLite Data to WinUI DataGrid and Perform CRUD Actions
2-
The repository maintains the demo example for how to bind SQLite data to WinUI DataGrid and perform CRUD actions easily.
2+
3+
This sample demonstrates how to bind SQLite database data to a WinUI DataGrid control and perform comprehensive CRUD (Create, Read, Update, Delete) operations with an intuitive user interface.
4+
5+
## Overview
6+
7+
SQLite is a lightweight, file-based relational database that is ideal for desktop applications. In this sample, we demonstrate how to:
8+
- Store employee data in an SQLite database
9+
- Display the data in a WinUI DataGrid with automatic column generation
10+
- Perform CRUD operations through an interactive UI
11+
12+
## SQLite Database Setup
13+
14+
The sample uses SQLite-net-pcl to interact with the SQLite database. The database is initialized in the `DatabasePath` at the application startup:
15+
16+
```csharp
17+
public const string DatabaseFilename = "SQLiteDatabase.db";
18+
19+
public static string DatabasePath =>
20+
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), DatabaseFilename);
21+
```
22+
23+
The database automatically creates the employee table when the application starts:
24+
25+
```csharp
26+
public SQLiteDatabase()
27+
{
28+
_database = new SQLiteAsyncConnection(DatabasePath, Flags);
29+
_database.CreateTableAsync<Employee>();
30+
}
31+
```
32+
33+
## Data Binding to WinUI DataGrid
34+
35+
The WinUI DataGrid is populated from the SQLite database by loading employee records asynchronously. When the main window is activated, it fetches all employees from the database and binds them to the DataGrid:
36+
37+
```xml
38+
<dataGrid:SfDataGrid DataContext="{StaticResource employeeViewModel}"
39+
x:Name="sfDataGrid"
40+
AllowEditing="True"
41+
ColumnWidthMode="Auto"
42+
AutoGenerateColumns="True">
43+
```
44+
45+
The grid automatically generates columns based on the employee data properties and displays all records with auto-fit column widths.
46+
47+
## CRUD Operations
48+
49+
### Create (Add New Record)
50+
51+
Users can add new employee records by clicking the **Add** option from the context menu. This opens the `AddOrEditWindow` dialog:
52+
53+
```csharp
54+
private void OnAddMenuClick(object sender, RoutedEventArgs e)
55+
{
56+
AddOrEditWindow addWindow = new AddOrEditWindow();
57+
addWindow.Title = "Add Record";
58+
App.ShowWindowAtCenter(addWindow.AppWindow, 550, 650);
59+
addWindow.Activate();
60+
}
61+
```
62+
63+
The form allows entering employee details like ID, Name, Email, Birth Date, Gender, and Location. When saved, the new record is inserted into the SQLite database:
64+
65+
```csharp
66+
if (isEdit)
67+
await App.Database.UpdateEmployeeAsync(SelectedRecord);
68+
else
69+
await App.Database.AddEmployeeAsync(SelectedRecord);
70+
```
71+
72+
### Read (Display Records)
73+
74+
All employee records are fetched from the SQLite database and automatically displayed in the DataGrid:
75+
76+
```csharp
77+
sfDataGrid.ItemsSource = await App.Database.GetEmployeesAsync();
78+
```
79+
80+
The `GetEmployeesAsync()` method returns all employee records from the database, which are then bound to the DataGrid for display.
81+
82+
### Update (Edit Existing Record)
83+
84+
Users can edit existing employee records by selecting a record and clicking the **Edit** option:
85+
86+
```csharp
87+
private void OnEditMenuClick(object sender, RoutedEventArgs e)
88+
{
89+
AddOrEditWindow editWindow = new AddOrEditWindow();
90+
editWindow.Title = "Edit Record";
91+
editWindow.SelectedRecord = sfDataGrid.SelectedItem as Employee;
92+
App.ShowWindowAtCenter(editWindow.AppWindow, 550, 650);
93+
editWindow.Activate();
94+
}
95+
```
96+
97+
The same form is reused for editing. When saved, the changes are updated in the database:
98+
99+
```csharp
100+
await App.Database.UpdateEmployeeAsync(SelectedRecord);
101+
```
102+
103+
### Delete (Remove Record)
104+
105+
Users can delete employee records by selecting a record and clicking the **Delete** option:
106+
107+
```csharp
108+
private void OnDeleteMenuClick(object sender, RoutedEventArgs e)
109+
{
110+
DeleteWindow deleteWindow = new DeleteWindow();
111+
App.ShowWindowAtCenter(deleteWindow.AppWindow, 200, 500);
112+
deleteWindow.SelectedRecord = sfDataGrid.SelectedItem as Employee;
113+
deleteWindow.Activate();
114+
}
115+
```
116+
117+
A confirmation dialog appears before deleting. Upon confirmation, the record is removed from the SQLite database:
118+
119+
```csharp
120+
private async void OnYesClick(object sender, RoutedEventArgs e)
121+
{
122+
await App.Database.DeleteEmployeeAsync(this.SelectedRecord);
123+
this.Close();
124+
}
125+
```
126+
127+
## Key Features
128+
129+
- **Async Database Operations**: All database operations are asynchronous for better application responsiveness
130+
- **Auto-Generated Columns**: The DataGrid automatically generates columns based on employee data properties
131+
- **Persistent Storage**: All data is persistently stored in the local SQLite database file
132+
133+
## Documentation
134+
Take a moment to peruse the [WinUI SfDataGrid Documentation](https://help.syncfusion.com/winui/datagrid/overview), where you can find about SfDataGrid with code examples.

0 commit comments

Comments
 (0)