gpt4 book ai didi

c# - 从 mysql 填充数据网格特定定义的列

转载 作者:行者123 更新时间:2023-11-29 02:48:15 24 4
gpt4 key购买 nike

我有一个包含 7 个定义列的 DataGrid,我想从 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';

最佳答案

只需将 AutoGenerateColumns 设置为 false

关于c# - 从 mysql 填充数据网格特定定义的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39184229/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com