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

Asked

Viewed 43 times

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)?

1 answer

1


The solution is very simple!

You need to set the attribute in XAML CanUserAddRows="False". This will prevent 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 Datagrid.A user can enter a new item to the blank line. Adding a new line adds an item to Itemssource.You can set default values for the new item by treating the Initializingnewitem event and setting values programmatically.

https://msdn.microsoft.com/pt-br/library/system.windows.controls.datagrid.canuseraddrows(v=vs.110). aspx

Browser other questions tagged

You are not signed in. Login or sign up in order to post.