- Home
- Forum
- Xamarin.Forms
- DataTable as Datasource in Xamrin.Forms
DataTable as Datasource in Xamrin.Forms
I'm using Xamrin.Forms Shared Project since I need to use System.Data.DataTable to connect to Remote SQL Server Database
I would like to know how to set DataTable as ItemsSource
Here is my code
<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Learning"
xmlns:sync="clr-namespace:Syncfusion.SfDataGrid.XForms;assembly=Syncfusion.SfDataGrid.XForms"
x:Class="Learning.LearningPage">
<ContentPage.Content>
<StackLayout>
<Button x:Name="btnGetData" Text="Get Data" Clicked="btn_Clicked"/>
<sync:SfDataGrid x:Name="LV1"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>
C# Code
using Xamarin.Forms;
using System;
using System.Data;
namespace Learning
{
public partial class LearningPage : ContentPage
{
public LearningPage()
{
InitializeComponent();
}
void btn_Clicked(object sender, System.EventArgs e)
{
if (sender == btnGetData)
{
try
{
var db = new DB_SQL(BaseDBFileName);
db.CheckConn();
DisplayAlert(GSVar.AppName, "Connection Successful", "OK");
var dt = new DataTable();
dt = db.ReadDT("SELECT * FROM File_Details");
DisplayAlert(GSVar.AppName, dt.Rows.Count.ToString(), "OK");
LV1.AutoGenerateColumns = true;
LV1.ItemsSource = dt;
} catch (Exception ex) { DisplayAlert(AppName, ex.Message, "OK"); }
}
}
}
}
SIGN IN To post a reply.
7 Replies
DS
Divakar Subramaniam
Syncfusion Team
August 19, 2016 08:36 AM UTC
Hi Amit,
Thanks for contacting Syncfusion Support.
We have checked your query. At present, Xamarin.Forms does not have support to access System.Data in PCL. SfDataGrid is a PCL library and hence we cannot access System.Data in it. Thus, it is not possible to bind a DataTable collection to SfDataGrid in Xamarin.Forms platform.
Regards,
Divakar.
AS
Amit Saraf
August 19, 2016 08:43 AM UTC
Thanks for your reply
I have not used observable collection or MVVM so can you please give me any sample so that I can convert data from datatable to observable collection
Please note that in my project there will be approx 20 datatable are to be used so please suggest me accordingly
I 'm using shared projects so that I can use DataTable and there is no other reason for using it
DS
Divakar Subramaniam
Syncfusion Team
August 19, 2016 11:13 AM UTC
Hi Amit,
We have prepared a sample binding an ObservableCollection to SfDataGrid with MVVM approach applied for your reference and you can download the same from the below link.
Please refer the below article about how to convert a DataTable to a List or an ObservableCollection.
Regards,
Divakar.
AS
Amit Saraf
August 19, 2016 12:03 PM UTC
Thanks for your help
Got it working.
Now How can I get selected row and read it in code behind
DS
Divakar Subramaniam
Syncfusion Team
August 19, 2016 01:31 PM UTC
Hi Amit,
We have checked your query and you can get the selected item by using SfDataGrid.SelectedItem property whenever you need. Please refer the below code example.
Get selected data using SelectedItem property:
|
SfDataGrid dataGrid = new SfDataGrid();
dataGrid.SelectionMode = SelectionMode.Single;
var selectedItem = dataGrid.SelectedItem; |
If you want to access them at the time of selection then you can get that by handling SfDataGrid.SelectionChanging or SfDataGrid.SelectionChanged event. Please refer the below code example.
Get selected data using SfDataGrid.SelectionChanged event:
|
dataGrid.SelectionChanged += DataGrid_SelectionChanged;
private void DataGrid_SelectionChanged(object sender, GridSelectionChangedEventArgs e)
{
var selectedItem = e.AddedItems;
} |
Please refer the below UG link to know more about the Selection events in SfDataGrid.
https://help.syncfusion.com/xamarin/sfdatagrid/selection#selection-events
https://help.syncfusion.com/xamarin/sfdatagrid/selection#selection-events
Regards,
Divakar.
AS
Amit Saraf
August 20, 2016 07:16 AM UTC
Thanks for your reply
This solves my query
Amit Saraf
DS
Divakar Subramaniam
Syncfusion Team
August 22, 2016 04:29 AM UTC
Hi Amit,
We are glad that your query is solved.
Please let us know if you need any other assistance.
Please let us know if you need any other assistance.
Regards,
Divakar.
SIGN IN To post a reply.
- 7 Replies
- 2 Participants
-
AS Amit Saraf
- Aug 18, 2016 06:35 AM UTC
- Aug 22, 2016 04:29 AM UTC