Empty line at the end when I do Data Binding - (WPF DataGrid Control + MySQL)

1

I've been implementing the example for this .

I basically 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 four records in the table, a fifth row appears on the (empty) grid?

    
asked by anonymous 03.11.2016 / 19:22

1 answer

1

The solution is quite simple!

You need to set the CanUserAddRows="False" attribute to XAML. This will prevent you from showing the empty line at the end.

 <DataGrid AutoGenerateColumns="False" CanUserResizeRows="False" CanUserAddRows="False" HorizontalAlignment="Left" Margin="14,55,0,46" Name="dataGridCustomers" Width="575" ItemsSource="{Binding Path=LoadDataBinding}">
  

When this property is set to true a blank line will be displayed at the bottom of the DataGrid. A user can type a new item in the blank row. Adding a new row adds an item to ItemsSource. You can set default values for the new item when handling the InitializingNewItem event and setting the values programmatically.

link

    
03.11.2016 / 20:24