1

I want to allow the user to add rows in a datagrid. I know the Datagridview from WinForms and there is allways on the bottom of the Datagrid a empty line that i can fill with data.

<DataGrid x:Name="dgv" 
          Grid.ColumnSpan="4" 
          Grid.Row="1" 
          CanUserAddRows="True" 
          CanUserDeleteRows="True" 
          SelectionUnit="FullRow" 
          AutoGenerateColumns="False" 
          ItemsSource="{Binding Entries}">
        <DataGrid.Columns>
            <DataGridComboBoxColumn Header="Anrede" Width="1*"></DataGridComboBoxColumn>
            <DataGridTextColumn Header="Vorname" Width="2*" Binding="{Binding Vorname}" />
            <DataGridTextColumn Header="Nachname" Width="2*" Binding="{Binding Nachname}" />
        </DataGrid.Columns>
    </DataGrid>

Code behind:

public ObservableCollection<Mitreisender> Entries { get; }

public aufenthaltsWindow()
{
    InitializeComponent();
    Entries = new ObservableCollection<Mitreisender>();
}

The Class

public class Mitreisender
{
    public int MitreisenderID { get; set; }
    public Gast.AnredeTyp Anrede { get; set; }
    public string Titel { get; set; }
    public string Vorname { get; set; }
    public string Nachname { get; set; }

    public virtual Aufenthalt Aufenthalt { get; set; }

    public Mitreisender()
    {

    }
}

2 Answers 2

5

Make sure that the Mitreisender class has a parameterless constructor:

public Mitreisender()
{

}

This is required for the blank "add" row to show up.

Also make sure that the binding works and that you have set the DataContext of the DataGrid or its parent window to an instance of the class where the Entries property is defined:

public aufenthaltsWindow()
{
    InitializeComponent();
    Entries = new ObservableCollection<Mitreisender>();
    DataContext = this;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Make sure that you have set the DataContext correctly. See my edit.
Yeah is was because the DataContext!! thanks for your help!
2

You need to set the DataGrid.ItemsSource to a modifyable collection. Suppose your DataContext contains a property

public ObservableCollection<EntryViewModel> Entries { get; }

with

class EntryViewModel // probably derive some ViewModelBase and implement INotifyPropertyChanged
{
    public string Vorname { get; set; }
    // ... other properties
}

Then bind the collection from datacontext to itemssource.

<DataGrid x:Name="dgv" ItemsSource="{Binding Entries}" ...>

With a modifyable collection and your settings, an extra line for new items will be available. You will still need to bind the columns to the actual item properties, I'm not addressing this part here.

3 Comments

@MathisHüttl Just to be sure... are you seeing some predefined items if you add them to the collection on initialization?
hmm... no i don't see them ... it looks like the binding doesn't work... right?
Yeeah... right. Seems like mm8 provided the missing pieces.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.