0
votes

I have mobile app project using Xamarin. Forums with MySql. I have page AboutPage.xaml :

<?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:d="http://xamarin.com/schemas/2014/forms/design"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                 mc:Ignorable="d"
                 x:Class="MyExpect.Views.AboutPage"
                 xmlns:vm="clr-namespace:MyExpect.ViewModels"
                 Title="{Binding Title}"
                 xmlns:dg="clr-namespace:Xamarin.Forms.DataGrid;assembly=Xamarin.Forms.DataGrid"
                 xmlns:conv="clr-namespace:DataGridSample.Views.Converters;assembly=DataGridSample">
        <ContentPage.BindingContext>
            <vm:AboutViewModel />
        </ContentPage.BindingContext>

        <ContentPage.Content>
            <dg:DataGrid x:Name="datagirdtable">         
            </dg:DataGrid>   
        </ContentPage.Content>
    </ContentPage>

and AboutPage.cs :

using System;
using System.ComponentModel;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace MyExpect.Views
{

    public partial class AboutPage : ContentPage
    {
        Connections matchs = new Connections();
        public AboutPage()

        {
            InitializeComponent();
            NavigationPage.SetHasNavigationBar(this, false);
            FillGirdView();
        }
        void FillGirdView() {
            datagirdtable.ItemsSource = matchs.ShowData();
        }
    }
}

and Connection Class :

using System;
    using System.Collections.Generic;
    using System.Text;
    using MySql.Data.MySqlClient;
    using System.Data;
    namespace MyExpect
    {
        class Connections
        {
            MySqlConnection connect = new 
            MySqlConnection("Server=localhost;Database=myexpect;uid=root;pwd=;");
            public DataTable ShowData() {
                DataTable dt = new DataTable();
                MySqlDataAdapter da = new MySqlDataAdapter("select * from matchs", connect);
                da.Fill(dt);
                return dt;
            }
        }
    }

I am trying to view Table Data on Gird Data Table ,but i receive this message : CS0266 C# Cannot implicitly convert type 'System.Data.DataTable' to 'System.Collections.IEnumerable'. An explicit conversion exists (are you missing a cast?)

1
first, connecting directly to a remote db from a mobile client is a horrible idea and you shouldn't do it. Second, as the error message says, a DataTable is not an IEnumerable. A DataGrid (and most other collection controls in Xamarin) expect IEnumerables for data sources. You will need to convert your DataTable into an IEnumerable. There are numerous examples of how to do this available on the web. - Jason

1 Answers

0
votes

@Jason I made these changes to get data from server ,I convert the table to json encode page then i made a model :-

using System;
using System.Collections.Generic;
using System.Text;

namespace NewMyExpect.Models
{
    class matchs
    {

        public string m_id { get; set; }
        public string S_id { get; set; }
        public string Weak { get; set; }
        public string League { get; set; }
        public string home { get; set; }
        public string away { get; set; }
        public string m_date { get; set; }
        public string homere { get; set; }
        public string awayre { get; set; }
        public string m_daytime { get; set; }
        public string m_time { get; set; }
        public string m_day { get; set; }

    }
}

and made list in the page which show table data :-

<?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:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="NewMyExpect.Views.AboutPage"
             Title="{Binding Title}">

    <ListView x:Name="MatchListView">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout Orientation="Horizontal">
                        <Label Text="{Binding m_daytime} " TextColor="Black"></Label>
                        <Label Text="{Binding m_day} " TextColor="Black"></Label>
                        <Label Text="{Binding m_time} " TextColor="Black"></Label>
                        <Label Text="{Binding home} " TextColor="Black"></Label>
                        <Label Text="{Binding away} " TextColor="Black"></Label>


                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

</ContentPage>

then make HttpClient in C# :

using System;
using System.ComponentModel;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using System.Text;
using System.Threading.Tasks;
using System.Linq;
using System.Collections.Generic;
using System.Net.Http;
using Newtonsoft.Json;

namespace NewMyExpect.Views
{
    // Learn more about making custom code visible in the Xamarin.Forms previewer
    // by visiting https://aka.ms/xamarinforms-previewer
    [DesignTimeVisible(false)]
    public partial class AboutPage : ContentPage
    {
        public AboutPage()
        {
            InitializeComponent();
            NavigationPage.SetHasNavigationBar(this, false);

            GetMatchs();
        }

        private async void GetMatchs()
        {
            HttpClient MyMatchs = new HttpClient();
            var responsematchs = await MyMatchs.GetStringAsync("https://technosat-iq.com/myexpect/activegames.php");
            var matchs = JsonConvert.DeserializeObject<List<Models.matchs>>(responsematchs);
            MatchListView.ItemsSource = matchs;
        }
    }
}

and this is the result :-

enter image description here