如何从mySql填充DataGrid

时间:2018-01-05 16:04:32

标签: c# mysql wpf visual-studio-2015 datagrid

使用MySql Workbench数据库中的数据填充DataGrid的最简单方法是什么? 数据库名称是pop-sf40-database,它看起来像这样: enter image description here

现在只有一个沙龙,我插入manualy。 (localhost root root)

型号: Salon.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace POP_SF_40_2014.Model
{
    class Salon
    {
        private int id;
        private string name;
        private string adress;
        private string telephone;
        private string email;
        private string web_site;
        private string tin;
        private string uid;
        private string bank_account;
        private bool deleted;

    }
}

GUI SalonWindow.xaml

<Window x:Class="POP_SF_40_2014.GUI.SalonWindow"
        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:POP_SF_40_2014.GUI"
        mc:Ignorable="d"
        Title="Салони" Height="500" Width="600">
    <Grid>
        <DataGrid x:Name="dgSalons" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="100" Width="572" SelectionChanged="dataGrid_SelectionChanged" LoadingRow="dgSalons_LoadingRow">
            <DataGrid.Columns>
                <DataGridTextColumn Header="ИД" Binding="{Binding Id}" Width="*"/>
                <DataGridTextColumn Header="Назив" Binding="{Binding Name}" Width="*"/>
                <DataGridTextColumn Header="Адреса" Binding="{Binding Adress}" Width="*"/>
                <DataGridTextColumn Header="Телефон" Binding="{Binding Email}" Width="*"/>
                <DataGridTextColumn Header="Интернет адреса" Binding="{Binding WebSite}" Width="*"/>
                <DataGridTextColumn Header="ПИБ" Binding="{Binding Tin}" Width="*"/>
                <DataGridTextColumn Header="Матични Број" Binding="{Binding Uid}" Width="*"/>
                <DataGridTextColumn Header="Жиро рачун" Binding="{Binding BankAccount}" Width="*"/>
            </DataGrid.Columns>
        </DataGrid>
        <Rectangle Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="285" Margin="15,119,0,0" Stroke="Black" VerticalAlignment="Top" Width="345"/>

        <Label x:Name="lblSalonId" Content="ИД" HorizontalAlignment="Left" Margin="15,126,0,0" VerticalAlignment="Top"/>
        <TextBox x:Name="txtSalonId" HorizontalAlignment="Left" Height="23" Margin="123,126,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="210" IsReadOnly="True"/>

        <Label x:Name="lblSalonName" Content="Назив" HorizontalAlignment="Left" Margin="15,152,0,0" VerticalAlignment="Top"/>
        <TextBox x:Name="txtSalonName" HorizontalAlignment="Left" Height="23" Margin="123,155,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="210"/>

        <Label x:Name="lblSalonAdress" Content="Адреса" HorizontalAlignment="Left" Margin="15,183,0,0" VerticalAlignment="Top"/>
        <TextBox x:Name="txtSalonAdress" HorizontalAlignment="Left" Height="23" Margin="123,186,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="210"/>

        <Label x:Name="lblSalonTelephone" Content="Телефон" HorizontalAlignment="Left" Margin="15,214,0,0" VerticalAlignment="Top"/>
        <TextBox x:Name="txtSalonTelephone" HorizontalAlignment="Left" Height="23" Margin="123,217,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="210"/>

        <Label x:Name="lblSalonEmail" Content="Е-пошта" HorizontalAlignment="Left" Margin="15,245,0,0" VerticalAlignment="Top"/>
        <TextBox x:Name="txtSalonEmail" HorizontalAlignment="Left" Height="23" Margin="123,248,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="210"/>

        <Label x:Name="lblSalonSite" Content="Интернет адреса" HorizontalAlignment="Left" Margin="15,276,0,0" VerticalAlignment="Top"/>
        <TextBox x:Name="txtSalonSite" HorizontalAlignment="Left" Height="23" Margin="123,279,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="210"/>

        <Label x:Name="lblSalonTin" Content="ПИБ" HorizontalAlignment="Left" Margin="15,307,0,0" VerticalAlignment="Top"/>
        <TextBox x:Name="txtSalonTin" HorizontalAlignment="Left" Height="23" Margin="123,310,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="210"/>

        <Label x:Name="lblSalonUid" Content="Матични број" HorizontalAlignment="Left" Margin="15,338,0,0" VerticalAlignment="Top"/>
        <TextBox x:Name="txtSalonUid" HorizontalAlignment="Left" Height="23" Margin="123,341,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="210"/>

        <Label x:Name="lblSalonBankAccount" Content="Жиро рачун" HorizontalAlignment="Left" Margin="15,369,0,0" VerticalAlignment="Top"/>
        <TextBox x:Name="txtSalonBankAccount" HorizontalAlignment="Left" Height="23" Margin="123,369,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="210"/>

        <Label x:Name="lblSalonSearch" Content="Претражи" HorizontalAlignment="Left" Margin="15,417,0,0" VerticalAlignment="Top"/>
        <TextBox x:Name="txtSalonSearch" HorizontalAlignment="Left" Height="23" Margin="123,420,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="210"/>

        <Button x:Name="btnAdd" Content="Додај" HorizontalAlignment="Left" Margin="365,119,0,0" VerticalAlignment="Top" Width="217"/>
        <Button x:Name="btnUpdate" Content="Измени" HorizontalAlignment="Left" Margin="365,146,0,0" VerticalAlignment="Top" Width="217"/>
        <Button x:Name="btnDelete" Content="Избриши" HorizontalAlignment="Left" Margin="365,173,0,0" VerticalAlignment="Top" Width="217"/>
        <Button x:Name="btnSave" Content="Сачувај" HorizontalAlignment="Left" Margin="365,382,0,0" VerticalAlignment="Top" Width="217"/>
        <Button x:Name="btnSearch" Content="Претражи" HorizontalAlignment="Left" Margin="365,421,0,0" VerticalAlignment="Top" Width="217"/>

    </Grid>
</Window>

它看起来像这样: enter image description here

此外,这是AppConfig

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name="POP_SF_40_2014.Properties.Settings.pop_sf40_databaseConnectionString1"
            connectionString="server=localhost;user id=root;database=pop-sf40-database"
            providerName="MySql.Data.MySqlClient" />
    </connectionStrings>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
</configuration>

1 个答案:

答案 0 :(得分:1)

您似乎必须创建一个数据访问类。从架构上讲,这将放在应用程序的数据访问层(DAL)上。

您必须使用此对象从数据库中检索信息并填充Salon对象。如果您正在使用DataGrid,那么这可能意味着您将处理沙龙对象的列表(ObservableCollection)。

Here is a good thread on best practices when writing a DAL