deru
deru

Reputation: 460

How do i get rid of an extra row created by Datagrid

I have two DataGrids in my WPF .net Application. One of them is working fine, the other one is creating one (empty) row I have no idea where it's added. The datagrids look very similar to me.

Here is how the grids are generated:

    <DataGrid AutoGenerateColumns="False" 
      Name="grid1">
        <DataGrid.Columns >
            <DataGridTextColumn Binding="{Binding Path=HUHU}" MinWidth="120" Header="HUHU" />
            ... some columns
        </DataGrid.Columns>
    </DataGrid>

    <DataGrid AutoGenerateColumns="False"
      Name="grid2" >
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Path=HU}" MinWidth="50" Header="HU" />
            ... some columns
        </DataGrid.Columns>
    </DataGrid>

Here is how the grids are filled:

    Dim grid1Source = New List(Of SomeType)

    For Each a As SomeType In MyFunctions.GetAllCalendarItems(CalendarPicker.SelectedDate)
        grid1Source.Add(a)
    Next
    grid1.ItemsSource = gird1Source

    Dim grid2Source = New List(Of SomeOtherType)

    For Each fa As SomeOtherTypeIn MyFunctions.GetsomeCalendarItems(CalendarPicker.SelectedDate)
        grid2Source.Add(fa)
    Next
    grid2.ItemsSource = grid2Source

Now the tricky thing: they are looking different. One of them is containing an empty row. (You can see it in the picture below) And it's not because of bad/empty Objects returned from the function (checked this about 500 times with the debugger).

this is how the DataGrids look like

Any ideas how to fix this?

Upvotes: 2

Views: 2594

Answers (1)

Muds
Muds

Reputation: 4116

Looks like this extra row is a new row that is provided by dataGrid. You need to mark CanUserAddRows = False to stop this feature.

this will stop grid from letting user add a new row.

ALso, read this for more info on how CanUserAddRows works

CanUserAddRows True if the DataGrid is not ReadOnly and IsEnabled, CanUserAddRows is set to true and IEditableCollectionView.CanAddNew is true

Upvotes: 6

Related Questions