从mysql中填充数据网格特定的定义列

本文关键字:定义 网格 数据网 mysql 填充 数据 | 更新日期: 2023-09-27 18:04:13

我有一个DataGrid,有7个定义的列,我想从MySQL数据库填充4个主题,所以我尝试了这个:

XAML

<UserControl x:Class="Stock.Resource.Transaction.Vente"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mui="http://firstfloorsoftware.com/ModernUI"
         xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:Stock.Resource.Transaction"
         mc:Ignorable="d" 
         d:DesignHeight="530" d:DesignWidth="1218">
<Grid>
    <DatePicker x:Name="Date" HorizontalAlignment="Left" Margin="10,94,0,0" VerticalAlignment="Top" Width="200"/>
    <ComboBox x:Name="Vendeur" Controls:TextBoxHelper.Watermark="Selectionner un Vendeur" HorizontalAlignment="Left" Margin="10,151,0,0" VerticalAlignment="Top" Width="200"/>
    <Button x:Name="Effect" Content="Effectué" HorizontalAlignment="Left" Margin="135,211,0,0" VerticalAlignment="Top" Width="75" Click="Effect_Click"/>
    <DataGrid x:Name="Produit" HorizontalAlignment="Left" Margin="240,64,0,0" VerticalAlignment="Top" MaxHeight="456" MaxWidth="968">
        <DataGrid.Columns>
            <DataGridTextColumn IsReadOnly="True" Header="Produit" Binding="{Binding Produit}"/>
            <DataGridTextColumn IsReadOnly="True" Header="Stock" Binding="{Binding Stock}"/>
            <DataGridTextColumn IsReadOnly="True" Header="Prix" Binding="{Binding Prix}"/>
            <DataGridTextColumn Header="Quantié"/>
            <DataGridTextColumn IsReadOnly="True" Header="Prix Unitaire" Binding="{Binding Prix Unitaire}"/>
            <DataGridTextColumn Header="Unitée"/>
            <DataGridTextColumn IsReadOnly="True" Header="Totale"/>
        </DataGrid.Columns>
    </DataGrid>
</Grid>
</UserControl>
c#

public void FillGrid(DataGrid data, string query)
{
        try
        {
            MySqlConnection connection = new MySqlConnection(ConStr().ToString());
            connection.Open();
            MySqlCommand cmd = new MySqlCommand(query, connection);
            cmd.ExecuteNonQuery();
            MySqlDataAdapter da = new MySqlDataAdapter(cmd);
            DataTable dt = new DataTable("concour");
            da.Fill(dt);
            data.ItemsSource = dt.DefaultView;
            da.Update(dt);
            connection.Close();
        }
        catch (Exception ex)
        {
            var v = new ModernDialog
            {
                Title = "Error",
                Content = ex.Message
            };
            v.Buttons = new Button[] { v.OkButton };
            var r = v.ShowDialog();
        }
}

它工作得很好,只是我得到了我想要填充的双列

这是我使用的查询:

SELECT 
    designation As 'Produit', stock As 'Stock', 
    prix As 'Prix', prixu As 'Prix Unitaire' 
FROM 
    produit 
WHERE 
    DELETED = 'N';

从mysql中填充数据网格特定的定义列

只需将AutoGenerateColumns设置为false