Ошибка приведения из Visual Studio при загрузке данных из MySql в таблицу сетки данных - PullRequest
0 голосов
/ 15 апреля 2020

У меня есть проект мобильного приложения с использованием Xamarin. Форумы с MySql. У меня есть страница 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>

и 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();
        }
    }
}

и класс соединения:

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;
            }
        }
    }

Я пытаюсь просмотреть данные таблицы в таблице данных Gird, но я получаю это сообщение: CS0266 C# Невозможно неявно преобразовать тип 'System.Data.DataTable' в 'System.Collections.IEnumerable'. Существует явное преобразование (вам не хватает приведения?)

1 Ответ

0 голосов
/ 18 апреля 2020

@ Джейсон Я внес эти изменения, чтобы получить данные с сервера, я преобразовал таблицу в json страницу кодирования, затем я сделал модель: -

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; }

    }
}

и создал список на странице, которая показывает таблицу данные: -

<?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>

, затем создайте HttpClient в 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;
        }
    }
}

, и это будет результат: -

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...