How to display data from Azure SQL Database in .NET MAUI DataGrid?
Displaying data from an Azure SQL Database in a .NET MAUI DataGrid involves several steps: setting up your Azure SQL Database, connecting to it from your .NET MAUI application, and displaying the retrieved data in a DataGrid. Below is a step-by-step guide to help you achieve this.
Step 1: Set Up Azure SQL Database
1. Create an Azure SQL Database:
- Log into the Azure Portal.
- Navigate to “SQL Databases” and click “Add”.
- Fill in the required details (Database name, server, resource group, etc.) and create the database.
- After creation, go to the database’s “Connection strings” and copy the ADO.NET connection string for later use.
2. Configure Firewall:
- Go to your database settings in the Azure Portal.
- Under “Firewalls and virtual networks”, add your IP address to allow access to the database.
Step 2: Connect to Azure SQL Database
-
Install the Microsoft.Data.SqlClient package to interact with SQL Server
-
Create a Data Access Layer.
C#
Create a class for accessing the database and retrieving data. Use the ADO.NET connection string copied earlier.
public class DatabaseService
{
public const string ConnectionString = "Your connection string here";
public IEnumerable<Stocks> PopulateData()
{
var items = new ObservableCollection<Stocks>();
using (SqlConnection conn = new SqlConnection(ConnectionString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand("SELECT Ticker, CompanyName, LastPrice, Change, ChangePercent, OpenColumn,HighColumn,LowColumn,Volume,MarketCap FROM Stocks", conn))
{
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
items.Add(new Stocks
{
Ticker = reader["Ticker"].ToString(),
CompanyName = reader["CompanyName"].ToString(),
LastPrice = Convert.ToDecimal(reader["LastPrice"]),
Change = Convert.ToDecimal(reader["Change"]),
ChangePercent = Convert.ToDecimal(reader["ChangePercent"]),
OpenColumn = Convert.ToDecimal(reader["OpenColumn"]),
HighColumn = Convert.ToDecimal(reader["HighColumn"]),
LowColumn = Convert.ToDecimal(reader["LowColumn"]),
Volume = Convert.ToInt64(reader["Volume"]),
MarketCap = Convert.ToDecimal(reader["MarketCap"])
});
}
}
}
return items;
}
}
}
ViewModel
Populate the data in the OnAppearing() method.
public void InitialyzeAsync()
{
foreach (var item in _service.PopulateData())
{
StockData.Add(item);
}
}
XAML
<syncfusion:SfDataGrid x:Name="sfGrid" ColumnWidthMode="Auto"
ItemsSource="{Binding StockData}"/>
The following screenshot shows how to display data from Azure SQL Database in .NET MAUI DataGrid.
Download the complete sample from GitHub.
Conclusion
I hope you enjoyed learning how to display data from an Azure SQL Database in .NET MAUI DataGrid.
You can refer to our .NET MAUI DataGrid feature tour page to learn about its other groundbreaking feature representations. Explore our .NET MAUI DataGrid Documentation to understand how to present and manipulate data.
For current customers, check out our .NET MAUI components from the License and Downloads page. If you are new to Syncfusion®, try our 30-day free trial to check out our .NET MAUI DataGrid and other .NET MAUI components.
Please let us know in the comments section if you have any queries or require clarification. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!