### Get Data Range from Worksheet
Source: https://github.com/devexpress-examples/maui-data-grid/blob/24.2.3+/CS/ImportExcelData/README.md
Access the first worksheet and use `GetDataRange` to determine the cells containing data. Validate the data structure before proceeding.
```csharp
private async Task LoadDataFromExcel(string filePath) {
// ...
// get the first sheet in the uploaded Excel file
Worksheet firstWorkSheet = newCustomersWorkbook.Worksheets[0];
// get the range of cells (TopRowIndex and LeftColumnIndex)
CellRange valuesRange = firstWorkSheet.GetDataRange();
int topRowIndex = valuesRange.TopRowIndex;
int leftColumnIndex = valuesRange.LeftColumnIndex;
// validate the received sheet and range of cells
if (!IsValidDataStructure(firstWorkSheet, topRowIndex, leftColumnIndex)) {
errorHandler?.Invoke("Data structure in the selected file is invalid");
return;
}
// ...
}
```
--------------------------------
### Configure MauiAsset Build Action
Source: https://github.com/devexpress-examples/maui-data-grid/blob/24.2.3+/CS/GridDatafromFireBase/Resources/Raw/AboutAssets.txt
Add this to your `.csproj` file to deploy raw assets from the Resources\Raw directory and its subdirectories. The `LogicalName` ensures the asset is deployed with its original relative path.
```xml
```
--------------------------------
### Load Raw Asset from App Package
Source: https://github.com/devexpress-examples/maui-data-grid/blob/24.2.3+/CS/GridDatafromFireBase/Resources/Raw/AboutAssets.txt
Accesses a raw asset deployed with your application package. Ensure the asset path matches the `LogicalName` configured in the `.csproj`.
```csharp
async Task LoadMauiAsset()
{
using var stream = await FileSystem.OpenAppPackageFileAsync("AboutAssets.txt");
using var reader = new StreamReader(stream);
var contents = reader.ReadToEnd();
}
```
--------------------------------
### Implement EmployeeData class for MAUI DataGrid
Source: https://github.com/devexpress-examples/maui-data-grid/blob/24.2.3+/CS/DataGridExample/Readme.md
Defines a class to generate and store a collection of employee objects. Use this as a data source for the DataGrid component.
```csharp
public class EmployeeData {
void GenerateEmployees() {
ObservableCollection result = new ObservableCollection();
result.Add(
new Employee("Nancy Davolio") {
BirthDate = new DateTime(1978, 12, 8),
HireDate = new DateTime(2005, 5, 1),
Position = "Sales Representative",
Address = "98122, 507 - 20th Ave. E. Apt. 2A, Seattle WA, USA",
Phone = "(206) 555-9857",
Access = AccessLevel.User,
OnVacation = false
}
);
result.Add(
new Employee("Andrew Fuller") {
BirthDate = new DateTime(1965, 2, 19),
HireDate = new DateTime(1992, 8, 14),
Position = "Vice President, Sales",
Address = "98401, 908 W. Capital Way, Tacoma WA, USA",
Phone = "(206) 555-9482",
Access = AccessLevel.Admin,
OnVacation = false
}
);
result.Add(
new Employee("Janet Leverling") {
BirthDate = new DateTime(1985, 8, 30),
HireDate = new DateTime(2002, 4, 1),
Position = "Sales Representative",
Address = "98033, 722 Moss Bay Blvd., Kirkland WA, USA",
Phone = "(206) 555-3412",
Access = AccessLevel.User,
OnVacation = false
}
);
result.Add(
new Employee("Margaret Peacock") {
BirthDate = new DateTime(1973, 9, 19),
HireDate = new DateTime(1993, 5, 3),
Position = "Sales Representative",
Address = "98052, 4110 Old Redmond Rd., Redmond WA, USA",
Phone = "(206) 555-8122",
Access = AccessLevel.User,
OnVacation = false
}
);
result.Add(
new Employee("Steven Buchanan") {
BirthDate = new DateTime(1955, 3, 4),
HireDate = new DateTime(1993, 10, 17),
Position = "Sales Manager",
Address = "SW1 8JR, 14 Garrett Hill, London, UK",
Phone = "(71) 555-4848",
Access = AccessLevel.User,
OnVacation = true
}
);
result.Add(
new Employee("Michael Suyama") {
BirthDate = new DateTime(1981, 7, 2),
HireDate = new DateTime(1999, 10, 17),
Position = "Sales Representative",
Address = "EC2 7JR, Coventry House Miner Rd., London, UK",
Phone = "(71) 555-7773",
Access = AccessLevel.User,
OnVacation = false
}
);
result.Add(
new Employee("Robert King") {
BirthDate = new DateTime(1960, 5, 29),
HireDate = new DateTime(1994, 1, 2),
Position = "Sales Representative",
Address = "RG1 9SP, Edgeham Hollow Winchester Way, London, UK",
Phone = "(71) 555-5598",
Access = AccessLevel.User,
OnVacation = false
}
);
result.Add(
new Employee("Laura Callahan") {
BirthDate = new DateTime(1985, 1, 9),
HireDate = new DateTime(2004, 3, 5),
Position = "Inside Sales Coordinator",
Address = "98105, 4726 - 11th Ave. N.E., Seattle WA, USA",
Phone = "(206) 555-1189",
Access = AccessLevel.User,
OnVacation = true
}
);
result.Add(
new Employee("Anne Dodsworth") {
BirthDate = new DateTime(1980, 1, 27),
HireDate = new DateTime(2004, 11, 15),
Position = "Sales Representative",
Address = "WG2 7LT, 7 Houndstooth Rd., London, UK",
Phone = "(71) 555-4444",
Access = AccessLevel.User,
OnVacation = false
}
);
Employees = result;
}
public ObservableCollection Employees { get; private set; }
public EmployeeData() {
GenerateEmployees();
}
}
```
--------------------------------
### Populate Customer Data from Excel
Source: https://github.com/devexpress-examples/maui-data-grid/blob/24.2.3+/CS/ImportExcelData/README.md
Iterate through the data range of the worksheet to create and populate `Customer` objects. Assign the populated list to the `Customers` property.
```csharp
private async Task LoadDataFromExcel(string filePath) {
// ...
List newCustomers = new List();
for (int rowIndex = topRowIndex + 1; rowIndex < valuesRange.RowCount + topRowIndex; rowIndex++) {
Customer newCustomer = new Customer() {
FirstName = firstWorkSheet.Rows[rowIndex][leftColumnIndex].Value.TextValue,
LastName = firstWorkSheet.Rows[rowIndex][leftColumnIndex + 1].Value.TextValue,
Company = firstWorkSheet.Rows[rowIndex][leftColumnIndex + 2].Value.TextValue
};
newCustomers.Add(newCustomer);
}
Customers = newCustomers;
}
```
--------------------------------
### Load Raw Asset from Application Package
Source: https://github.com/devexpress-examples/maui-data-grid/blob/24.2.3+/CS/ImportExcelData/CS/Resources/Raw/AboutAssets.txt
Use this C# code to access and read the contents of a deployed raw asset file. Ensure the file is correctly included in your project using the MauiAsset build action.
```csharp
async Task LoadMauiAsset()
{
using var stream = await FileSystem.OpenAppPackageFileAsync("AboutAssets.txt");
using var reader = new StreamReader(stream);
var contents = reader.ReadToEnd();
}
```
--------------------------------
### Define Employee Data Model
Source: https://github.com/devexpress-examples/maui-data-grid/blob/24.2.3+/CS/DataGridExample/Readme.md
Create the data structure for the grid, including properties for employee details and image loading logic.
```cs
using System.Collections.ObjectModel;
using Microsoft.Maui.Controls;
using System;
namespace DataGridExample {
public enum AccessLevel {
Admin,
User
}
public class Employee {
string name;
public string Name {
get { return name; }
set {
name = value;
if (Photo == null && !String.IsNullOrEmpty(name))
Photo = ImageSource.FromFile(name.ToLower().Replace(" ", "_") + ".jpg");
}
}
public Employee(string name) {
this.Name = name;
}
public ImageSource Photo { get; set; }
public DateTime BirthDate { get; set; }
public DateTime HireDate { get; set; }
public string Position { get; set; }
public string Address { get; set; }
public string Phone { get; set; }
public AccessLevel Access { get; set; }
public bool OnVacation { get; set; }
}
```
--------------------------------
### Load Excel Document Async
Source: https://github.com/devexpress-examples/maui-data-grid/blob/24.2.3+/CS/ImportExcelData/README.md
Use the `LoadDocumentAsync` method to open an Excel file. Ensure proper error handling if the file cannot be opened.
```csharp
private async Task LoadDataFromExcel(string filePath) {
using Workbook newCustomersWorkbook = new Workbook();
bool excelOpenResult = await newCustomersWorkbook.LoadDocumentAsync(filePath);
if (!excelOpenResult) {
errorHandler?.Invoke("Couldn't open the file");
return;
}
// ...
}
```
--------------------------------
### Configure DataGrid Editor and Columns in XAML
Source: https://github.com/devexpress-examples/maui-data-grid/blob/24.2.3+/CS/DataGridExample/Readme.md
Defines a DataGrid with custom templates and column types, setting the editor invocation gesture to a double tap.
```xaml
```
--------------------------------
### Display Placeholders for Empty Groups in MAUI Data Grid
Source: https://github.com/devexpress-examples/maui-data-grid/blob/24.2.3+/CS/SingleColumnKanbanView/README.md
Configure the Data Grid to display placeholders for empty groups using Border controls and DataTriggers. This enhances user experience by indicating where items can be dropped.
```xaml
```
--------------------------------
### Handle Row Drag and Drop for Placeholder Management
Source: https://github.com/devexpress-examples/maui-data-grid/blob/24.2.3+/CS/SingleColumnKanbanView/README.md
Implement the CompleteRowDragDrop event to manage placeholders when rows are dragged and dropped. This involves adding a placeholder to the source group if it becomes empty and removing it from the target group if an item is dropped there.
```csharp
private void DataGridView_CompleteRowDragDrop(object sender, DevExpress.Maui.DataGrid.CompleteRowDragDropEventArgs e) {
AddPlaceholderTaskToSourceGroup();
RemovePlaceholderTaskFromTargetGroup();
}
void AddPlaceholderTaskToSourceGroup() {
if (!viewModel.Tasks.Any(t => t.Stage == draggedTaskOriginalStage)) {
viewModel.Tasks.Add(new TaskToDo() { IsPlaceholder = true, Stage = draggedTaskOriginalStage });
}
}
void RemovePlaceholderTaskFromTargetGroup() {
string newDraggedTaskStage = draggedItem.Stage;
TaskToDo stabTask = viewModel.Tasks.FirstOrDefault(t => t.Stage == newDraggedTaskStage && t.IsPlaceholder);
if (stabTask != null) {
viewModel.Tasks.Remove(stabTask);
}
}
```
--------------------------------
### Register DevExpress Controls in MauiProgram.cs
Source: https://github.com/devexpress-examples/maui-data-grid/blob/24.2.3+/CS/DataGridExample/Readme.md
Call the UseDevExpress method within the MauiApp builder to register necessary handlers for DevExpress controls.
```cs
using Microsoft.Maui;
using Microsoft.Maui.Controls.Hosting;
using Microsoft.Maui.Hosting;
using DevExpress.Maui;
namespace DataGridExample {
public static class MauiProgram {
public static MauiApp CreateMauiApp() {
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp()
.UseDevExpress()
.ConfigureFonts(fonts => {
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
});
return builder.Build();
}
}
}
```
--------------------------------
### Define EmployeeDataViewModel
Source: https://github.com/devexpress-examples/maui-data-grid/blob/24.2.3+/CS/DataGridExample/Readme.md
Implements INotifyPropertyChanged to provide data for the DataGrid.
```csharp
using System.Collections.Generic;
using System.ComponentModel;
namespace DataGridExample {
public class EmployeeDataViewModel : INotifyPropertyChanged {
readonly EmployeeData data;
public event PropertyChangedEventHandler PropertyChanged;
public IReadOnlyList Employees { get => data.Employees; }
public EmployeeDataViewModel() {
data = new EmployeeData();
}
protected void RaisePropertyChanged(string name) {
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
}
```
--------------------------------
### Declare DataGridView in XAML
Source: https://github.com/devexpress-examples/maui-data-grid/blob/24.2.3+/CS/DataGridExample/Readme.md
Add the DataGridView to your ContentPage by defining the dxg namespace prefix.
```xaml
```
--------------------------------
### Handle Custom Group Sorting in MAUI Data Grid
Source: https://github.com/devexpress-examples/maui-data-grid/blob/24.2.3+/CS/SingleColumnKanbanView/README.md
Implement the DataGridView.CustomSort event to sort groups in ascending or descending order based on custom logic. This is useful for defining a specific order for stages in a Kanban board.
```csharp
private void DataGridView_CustomSort(object sender, DevExpress.Maui.DataGrid.CustomSortEventArgs e) {
if (e.Column.FieldName == "Stage")
e.Result = Comparer.Default.Compare(viewModel.StageOrder[(string)e.Value1], viewModel.StageOrder[(string)e.Value2]);
}
```
--------------------------------
### Bind DataGrid to ViewModel
Source: https://github.com/devexpress-examples/maui-data-grid/blob/24.2.3+/CS/DataGridExample/Readme.md
Configures the BindingContext and ItemsSource for the DataGrid in XAML.
```xaml
```
--------------------------------
### Data Grid Column Chooser Popup Structure
Source: https://github.com/devexpress-examples/maui-data-grid/blob/24.2.3+/CS/ColumnChooser/README.md
Defines the structure of the column chooser popup using DXPopup and DXCollectionView. The DXCollectionView is bound to the DataGrid's columns, and each item displays a CheckEdit to control column visibility.
```xaml
```
--------------------------------
### Resize DXCollectionView to Content
Source: https://github.com/devexpress-examples/maui-data-grid/blob/24.2.3+/CS/ColumnChooser/README.md
Set the ReduceSizeToContent property to true on DXCollectionView to make it resize to fit its content. This is useful for optimizing space within the column chooser popup.
```xaml
```
--------------------------------
### Enable Drag-and-Drop in DataGrid
Source: https://github.com/devexpress-examples/maui-data-grid/blob/24.2.3+/CS/DataGridExample/Readme.md
Enables row reordering by setting the AllowDragDropRows property to True.
```xaml
```
--------------------------------
### Set Minimum Width for Data Grid Columns
Source: https://github.com/devexpress-examples/maui-data-grid/blob/24.2.3+/CS/ColumnChooser/README.md
Specifies the minimum width for Data Grid columns using the MinWidth property. This helps prevent horizontal scrollbars when the total width of visible columns is less than the viewport width.
```xaml
```
--------------------------------
### Bind CheckEdit Label to Column Caption
Source: https://github.com/devexpress-examples/maui-data-grid/blob/24.2.3+/CS/ColumnChooser/README.md
Binds the Label property of the CheckEdit component to the ActualCaption property of the GridColumn. This displays the column's caption next to the checkbox in the chooser.
```xaml
```
--------------------------------
### Align CheckEdit Checkboxes to the Right
Source: https://github.com/devexpress-examples/maui-data-grid/blob/24.2.3+/CS/ColumnChooser/README.md
Aligns the checkboxes of the CheckEdit component to the right within the column chooser item template. Use CheckBoxPosition.End for this alignment.
```xaml
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.