【发布时间】:2020-04-14 22:41:24
【问题描述】:
我有使用 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;
}
}
}
我正在尝试查看网格数据表上的表数据,但我收到此消息: CS0266 C# 无法将类型“System.Data.DataTable”隐式转换为“System.Collections.IEnumerable”。存在显式转换(您是否缺少演员表?)
【问题讨论】:
-
首先,从移动客户端直接连接到远程数据库是一个可怕的想法,您不应该这样做。其次,正如错误消息所说,DataTable 不是 IEnumerable。 DataGrid(以及 Xamarin 中的大多数其他集合控件)期望 IEnumerables 用于数据源。您需要将 DataTable 转换为 IEnumerable。网上有很多关于如何做到这一点的例子。
标签: c# mysql xaml xamarin datagrid