1
I have been implementing the example of this link.
Basically I have:
XAML
<Window x:Class="DataGridBind.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:DataGridBind"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="620">
<Grid Height="350" Width="625" Background="#FFD1F9EE" >
<TextBlock Height="32" HorizontalAlignment="Left" Margin="16,15,0,0" Name="textBlockHeading" Text="Produtos" VerticalAlignment="Top" Width="310" FontSize="20" FontStretch="Normal"/>
<Grid HorizontalAlignment="Left" Height="310" VerticalAlignment="Top" Width="625">
<DataGrid AutoGenerateColumns="False" HorizontalAlignment="Left" Margin="14,55,0,46" Name="dataGridCustomers" Width="575" ItemsSource="{Binding Path=LoadDataBinding}" CanUserResizeRows="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=idProduto}" Header="Código" Width="100" IsReadOnly="True" />
<DataGridTextColumn Binding="{Binding Path=ProdutoNome}" Header="Nome" Width="100" IsReadOnly="True" />
<DataGridTextColumn Binding="{Binding Path=ProdutoPU}" Header="Preço Unit." Width="100" IsReadOnly="True" />
<DataGridTextColumn Binding="{Binding Path=ProdutoStock}" Header="Stock" Width="100" IsReadOnly="True" />
<DataGridTextColumn Binding="{Binding Path=ProdutoStockMin}" Header="Stock Min." Width="100" IsReadOnly="True" />
<DataGridTextColumn Binding="{Binding Path=ProdutoStockMax}" Header="Stock Max." Width="100" IsReadOnly="True" />
</DataGrid.Columns>
</DataGrid>
<Button Content="Load Data" Height="25" HorizontalAlignment="Left" Margin="487,275,0,0" Name="btnloaddata" VerticalAlignment="Top" Width="100" Click="btnloaddata_Click"/>
</Grid>
</Grid>
code Behind
using System.Windows;
using System.Data;
using MySql.Data.MySqlClient;
using System.Configuration;
namespace DataGridBind
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
//var connectionString = ConfigurationManager.
#region MySqlConnection Connection
MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
public MainWindow()
{
InitializeComponent();
}
#endregion
#region bind data to DataGrid.
private void btnloaddata_Click(object sender, RoutedEventArgs e)
{
if(!loadData())
MessageBox.Show("Ocorreu um erro inesperado ...");
}
#endregion
private bool loadData()
{
bool status = false;
try
{
conn.Open();
MySqlCommand cmd = new MySqlCommand("Select idProduto, ProdutoNome, ProdutoPU, ProdutoStock, ProdutoStockMin, ProdutoStockMax from Produto", conn);
MySqlDataAdapter adp = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
adp.Fill(ds, "LoadDataBinding");
dataGridCustomers.DataContext = ds;
status = true;
}
catch (MySqlException ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
conn.Close();
}
return status;
}
}
}
Why is it that, having me four records in the table, I get a fifth line on the grid (empty)?