Alvin
Alvin

Reputation: 8499

Datagrid add row add a empty row by default

The function InsertProductItem will add arow into the datgrid. The code work, but it will add a empty row by default. How can I remove it?

Xaml:

<DataGrid Name="dgProductList" 
                                  ItemsSource="{Binding Path=ProductList}">

Code:

public ObservableCollection<SalesItem> _ProductList = new ObservableCollection<SalesItem>();
        public ObservableCollection<SalesItem> ProductList { get { return _ProductList; } set { _ProductList = value; } }

        public SalesWindow()
        {
            InitializeComponent();

            this.DataContext = this;
        }

public void InsertProductItem(Product product)
        {
            SalesItem item = new SalesItem { ProductName = product.Name, Quantity = 1, TotalPrice = product.Price };
            ProductList.Add(item);
        }

Thank you.

Upvotes: 0

Views: 2143

Answers (1)

Rachel
Rachel

Reputation: 132548

Set CanUserAddRows to False on your DataGrid if you don't want to use the DataGrid's default way of adding rows

<DataGrid Name="dgProductList" 
          ItemsSource="{Binding Path=ProductList}"
          CanUserAddRows="False">

Upvotes: 4

Related Questions