WPF datagrid empty row at bottom

WpfDatagrid

Wpf Problem Overview


I bind my datagrid using

//fill datagrid
public DataTable GameData
{
    get
    {
        DataSet ds = new DataSet();
        FileStream fs = new FileStream(IMDB.WebPage.Class.Config.XMLPath,
        FileMode.Open, FileAccess.Read);
        StreamReader reader = new StreamReader(fs, Encoding.Default);
        ds.ReadXml(reader);
        fs.Close();
        DataTable temp = ds.Tables[0];
        return ds.Tables[0];
     }
 }

For some reason I get an empty row at the bottom. And sometimes after clicking on some buttons and checkboxes in the grid, more empty rows are added.

Why is this? And how do I block this?

Wpf Solutions


Solution 1 - Wpf

Sounds like you probably have CanUserAddRows set to true for the DataGrid. Just add

CanUserAddRows="false"

to the XAML.

Solution 2 - Wpf

It also works with the attribute:

IsReadOnly="true"

Solution 3 - Wpf

If your backing collection that implements IEditableCollectionView returns true from its CanAddNew method, the DataGrid will assume that is what you want to do.

There's a good overview here:Overview of the editing features in the WPF DataGrid

Solution 4 - Wpf

If you're creating DataGrid on-the-fly through Source Code...

DataGrid grid = new DataGrid();

grid.CanUserAddRows = false;

//... 
grid.AutoGenerateColumns = false;
grid.Margin = new Thickness(10,20,10,10);
grid.VerticalAlignment = VerticalAlignment.Top;
grid.ItemsSource = //... and so on

Solution 5 - Wpf

Though the OP was asking how to REMOVE the empty row, the title isn't specific, and this article appeared in my search while trying to figure out how to ADD the empty row. I found that, for the empty row to appear, it not only needs to have CanUserAddRows="True" but the ItemsSource needs to have a default constructor public MyClass () { }.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWtFudgEView Question on Stackoverflow
Solution 1 - WpfTomi JunnilaView Answer on Stackoverflow
Solution 2 - WpfArtiView Answer on Stackoverflow
Solution 3 - WpfAlastair MawView Answer on Stackoverflow
Solution 4 - WpfFrancisco CamposView Answer on Stackoverflow
Solution 5 - WpfAdvAppView Answer on Stackoverflow