我有一个带有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';
答案 0 :(得分:1)
所有需要的是将AutoGenerateColumns
设置为false
。