使用C#和Mysql进行数据绑定显示错误的数据

时间:2019-06-01 07:43:15

标签: c# wpf xaml

我刚刚创建了我的第一个WPF应用程序并将其连接到MySql数据库。 我可以从数据库中检索数据,但是它完全错误

我通过互联网和不同的线程进行了搜索,现在我尝试了几个小时的不同尝试。

我的XAML文件

   <Window x:Class="AppGetMySQL.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:AppGetMySQL"
    mc:Ignorable="d"
    Title="Get All Records" Height="450" WindowStartupLocation="CenterScreen" Width="800" AllowsTransparency="False" WindowStyle="ThreeDBorderWindow">
<Window.Background>
    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
        <GradientStop Color="#FFF1F7FF" Offset="0"/>
        <GradientStop Color="White" Offset="1"/>
    </LinearGradientBrush>
</Window.Background>
<Grid Margin="0,-52,0,0">
    <Button Content="Connect" HorizontalAlignment="Left" Margin="10,96,0,0" VerticalAlignment="Top" Width="112" Height="41" Click="Button_Click"/>
    <ListView HorizontalAlignment="Left" Margin="126,170,0,0" VerticalAlignment="Top" Name="ListAllUsers" Width="527" Height="291" FontSize="16" FontFamily="Calibri">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Id" />
                <GridViewColumn Header="Name" Width="300px"/>
                <GridViewColumn Header="City" Width="300px" />
            </GridView>
        </ListView.View>
    </ListView>

</Grid>
</Window>

我的.cs文件

int x = 0;

string connectionString = "SERVER=db5.freemysqlhosting.net;" + "DATABASE=db5233941;" + "UID=db5233941;" + "PASSWORD=*******;";
        string query = "select * from AllUsers";//I know I should not use *, but this is just test.
        MySqlConnection con = new MySqlConnection(connectionString);
        MySqlDataAdapter ada = new MySqlDataAdapter(query, con);
        DataTable dt = new DataTable();
        ada.Fill(dt);

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            DataRow dr = dt.Rows[i];
            List<String> stringList = new List<string>();
            stringList.Add(dr["Id"].ToString());
            stringList.Add(dr["Name"].ToString());
            stringList.Add(dr["City"].ToString());
            ListAllUsers.ItemsSource = stringList;
        }

我希望打印以下内容:

  

| id |姓名|城市|

     

| id |姓名|城市|

     

| id |姓名|城市|

但是它打印

  

| id | id | id |

     

|姓名|姓名|名称|

     

|城市|城市|城市|

图片:

How it gets printed

我在做什么错了?

编辑: 实际上,当我将其添加到循环中时:

            Console.WriteLine("Id: " + dr["Id"].ToString());
            Console.WriteLine("Name: " + dr["Name"].ToString());
            Console.WriteLine("City: " + dr["City"].ToString());

它可以正确打印所有内容。我的桌子上所有五行。

2 个答案:

答案 0 :(得分:0)

我认为您某种程度上缺少绑定。与本教程页面进行比较,可能会有所帮助。 ListView with a GridView摘录:

        <ListView.View>
            <GridView>
                <GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding Name}" />
                <GridViewColumn Header="Age" Width="50" DisplayMemberBinding="{Binding Age}" />
                <GridViewColumn Header="Mail" Width="150" DisplayMemberBinding="{Binding Mail}" />
            </GridView>
        </ListView.View>

答案 1 :(得分:0)

我解决了该问题,并在以后有人需要时发布了解决方案。

我在xaml中对此进行了更改:

                <GridView>
                <GridViewColumn Header="Id"   DisplayMemberBinding="{Binding Id}" />
                <GridViewColumn Header="Name" Width="300px"  DisplayMemberBinding="{Binding Name}"  />
                <GridViewColumn Header="City" Width="300px" DisplayMemberBinding="{Binding City}"  />
                </GridView>

并在我的.cs文件中使用了此

for (int i = 0; i < dt.Rows.Count; i++)
            {
            DataRow dr = dt.Rows[i];
            ListAllUsers.Items.Add(new User() {Id = Convert.ToInt32(dr["Id"]), Name = dr["Name"].ToString(), City = dr["City"].ToString() });
            ...

这就是我需要改变的。

希望下一个会对此有所需求。