How to remove DataGrid's blank row when binding to a ObservableCollection<T>?

37,767

Solution 1

I've got it

on Datagrid XAML, put the attribute:

IsReadOnly="True"

Solution 2

The same problem persist in WPF 4.0 version of DataGrid, and it is caused by the add-new row which it shows automatically for ObservableCollection ItemsSource. Setting IsReadOnly as True it's too radical IMHO.
I solved it by disabling CanUserAddRows property if you don't need that behavior, but you still want cells to be modified:

CanUserAddRows="False"

Solution 3

CanUserAddRows="False" and IsReadOnly="True" combination of both is better to ensure any additional inconveniences.

Share:
37,767

Related videos on Youtube

Junior Mayhé
Author by

Junior Mayhé

Brazilian Software Engineer graduated in Information Systems. Fluent in English and Spanish. ITIL and SCRUM certificated professional. Solid experience throughout the life cycle of software development. Working for over 15 years designing, building and deploying solutions for areas like Human Resources, Finance, Publishing, Telecommunications, Entertainment, Healthcare, Logistics and Energy. International experience interacting with professionals from several countries like Italy, Belgium, Portugal, Holland, USA, Colombia, Spain and Mexico. 10 years of experience in systems analysis, requirements gathering, object-oriented analysis, data modeling, technical specification. 15 years in systems development, maintenance, testing for national and international companies. 4 years interacting with professionals from different countries, of which 2 years playing technical project leadership role abroad, delegating, and following up tasks. 3 years negotiating with infrastructure and service providers. 2 years defining the IT tools and infrastructure to be implemented and processes to be improved, covering software, hardware, communications needs, to meet the needs of the organization. 2 years organizing and supervising the acquisition and update of computer systems, whether hardware or software (licenses, servers, contracts). Specialties: Systems Analysis, Requirements Analysis, Functional Analysis, Software Development, Project Management, SCRUM Agile methodology, Waterfall methodology, ITIL, Software Testing, Continuous Integration, C# programming language, ASP.NET MVC, Entity Framework, Windows Forms, WPF, WCF, .NET Core, ASP.NET Web API, JavaScript, Angular 6, TypeScript, JQuery, HTML5, CSS3, XML, XSD, XAML, SQL Server, PHP, MySQL, MongoDB, BPM, UML, GIT, Prototyping, Database Modeling and Administration Personal Site | Linkedin profile

Updated on September 07, 2020

Comments

  • Junior Mayhé
    Junior Mayhé almost 4 years

    I'm getting nuts here with this:

    ObservableCollection<Employee> list = new ObservableCollection<Employee>();
    dgEmployees.ItemsSource = list;
    

    When you debug the list variable, it's empty (list.Count =0), but then I bind it to a DataGrid (WPFToolkit), it shows me a blank row.

    In immediate window, for dgEmployees.Items it's showing:

    dgEmployees.Items[0]
    {NewItemPlaceholder}
    

    and

    dgEmployees.Items[0].GetType()
    {Name = "NamedObject" FullName = "MS.Internal.NamedObject"}
    [System.RuntimeType]: {Name = "NamedObject" FullName = "MS.Internal.NamedObject"}
    

    It seems to happen after I've put this Datagrid into a TabControl, but I'm not sure it has something to do with it.

    Does anyone know how to remove this blank row?

    • reggaeguitar
      reggaeguitar over 9 years
      You should accept the other answer, it's better than yours
    • ffonz
      ffonz over 2 years
      I got the exception System.InvalidCastException: 'Unable to cast object of type 'MS.Internal.NamedObject' to type MyClass in external code through one of my bindings. Found out this extra empty line was the culprit! But the answer of Pablonete was better for me.
  • Jon
    Jon over 13 years
    agreed unless it is truly readonly then that seems over kill.
  • Dessus
    Dessus about 11 years
    Thanks, this answer helped me a lot more than the IsReadOnly which in my case had some side effects that I didn't want.
  • Shreyas
    Shreyas over 9 years
    Can we mark this as the answer as IsReadOnly does have side-effects while this does not?
  • NappingRabbit
    NappingRabbit over 6 years
    you are suggesting he use two overlapping functionalities where either one will suffice and in fact IsReadOnly = true will remove other functionality with no gain at all.