Previous MAUI-SQLite-Connection MAUI-with-HttpClient Next

MAUI SQLite Full CRUD + Search Sample Project

The sample project to include full CRUD operations plus a search feature. This way, you’ll have a complete SQLite-backed Todo app in .NET MAUI.

📁 Project Structure

MyMauiApp/
│
├── Models/
│   └── TodoItem.cs
│
├── Services/
│   └── DatabaseService.cs
│
├── ViewModels/
│   └── TodoViewModel.cs
│
├── Views/
│   └── TodoPage.xaml
│   └── TodoPage.xaml.cs
│
├── MauiProgram.cs
└── App.xaml / App.xaml.cs

🧩 Code

1. Services/DatabaseService.cs (add search)

public Task<List<TodoItem>> SearchItemsAsync(string keyword) =>
    _database.Table<TodoItem>()
             .Where(i => i.Name.Contains(keyword))
             .ToListAsync();

2. ViewModels/TodoViewModel.cs (CRUD + search)

public class TodoViewModel : BindableObject
{
    private readonly DatabaseService _db;

    public ObservableCollection<TodoItem> Items { get; } = new();

    public TodoViewModel(DatabaseService db)
    {
        _db = db;
        LoadItems();
    }

    private async void LoadItems()
    {
        var items = await _db.GetItemsAsync();
        Items.Clear();
        foreach (var item in items)
            Items.Add(item);
    }

    public async Task AddItem(string name)
    {
        var newItem = new TodoItem { Name = name, IsCompleted = false };
        await _db.SaveItemAsync(newItem);
        LoadItems();
    }

    public async Task ToggleItem(TodoItem item)
    {
        item.IsCompleted = !item.IsCompleted;
        await _db.SaveItemAsync(item);
        LoadItems();
    }

    public async Task DeleteItem(TodoItem item)
    {
        await _db.DeleteItemAsync(item);
        LoadItems();
    }

    public async Task SearchItems(string keyword)
    {
        var results = await _db.SearchItemsAsync(keyword);
        Items.Clear();
        foreach (var item in results)
            Items.Add(item);
    }
}

3. Views/TodoPage.xaml (add search bar)

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             x:Class="MyMauiApp.Views.TodoPage"
             Title="Todo List">

    <VerticalStackLayout Padding="20">
        <!-- Add new item -->
        <Entry x:Name="NewItemEntry" Placeholder="Enter new task..." />
        <Button Text="Add" Clicked="OnAddClicked" />

        <!-- Search bar -->
        <Entry x:Name="SearchEntry" Placeholder="Search tasks..." />
        <Button Text="Search" Clicked="OnSearchClicked" />

        <!-- List of items -->
        <CollectionView ItemsSource="{Binding Items}">
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <StackLayout Orientation="Horizontal" Padding="5">
                        <Label Text="{Binding Name}" VerticalOptions="Center" />
                        <CheckBox IsChecked="{Binding IsCompleted}" />
                        <Button Text="Delete" CommandParameter="{Binding .}" />
                    </StackLayout>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>
    </VerticalStackLayout>
</ContentPage>

4. Views/TodoPage.xaml.cs (wire up search)

public partial class TodoPage : ContentPage
{
    private readonly TodoViewModel _vm;

    public TodoPage(TodoViewModel vm)
    {
        InitializeComponent();
        BindingContext = _vm = vm;
    }

    private async void OnAddClicked(object sender, EventArgs e)
    {
        if (!string.IsNullOrWhiteSpace(NewItemEntry.Text))
        {
            await _vm.AddItem(NewItemEntry.Text);
            NewItemEntry.Text = string.Empty;
        }
    }

    private async void OnSearchClicked(object sender, EventArgs e)
    {
        if (!string.IsNullOrWhiteSpace(SearchEntry.Text))
        {
            await _vm.SearchItems(SearchEntry.Text);
        }
        else
        {
            // reload all items if search is empty
            await _vm.SearchItems(string.Empty);
        }
    }
}

✅ Features Covered

  • Create: Add new tasks.
  • Read: Display all tasks.
  • Update: Toggle completion status.
  • Delete: Remove tasks.
  • Search: Filter tasks by keyword.

This gives you a complete CRUD + search Todo app in MAUI with SQLite.

Back to Index
Previous MAUI-SQLite-Connection MAUI-with-HttpClient Next
*