6

I have the following DataGrid:

 <DataGrid ItemsSource="{Binding EmployeeList}" CanUserAddRows="True" AutoGenerateColumns="False" Margin="0,0,0,90">
        <DataGrid.Columns>
            <DataGridTemplateColumn Header="CountryCombo2">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ComboBox ItemsSource="{Binding Path=DataContext.CountryList, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" 
                                      DisplayMemberPath="CountryName" 
                                      SelectedItem="{Binding EmployeeCountry, Mode=TwoWay}"
                                      SelectedValue="{Binding EmployeeCountry.CountryId}"
                                      SelectedValuePath="CountryId" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

However, I am unable to add new rows to the DataGrid. Please let me know if I need to provide any additional code.

Update :

Screen 1 : This is the screenshot when the window is just loaded with the hardcoded property values. Now I see the empty new row.

Screen 1

Screen 2 : Here I have added data into the new row with values Rambo and Russia. Now, no matter what I do (tab-out, click in another cell), the next new row is not added. I believe it should be adding a new row.

Screen 2

Screen 3 : Here the newly added row values have disappeared. That is because I double clicked on the thin border between the two empty cells. Now this is pretty weird.

Screen 3

Screen 4 : Now when I click on the Peter cell, the previously entered row data is back but now it is pushed down and a new empty row is inserted before it. This is very strange.

Screen 3

Can anyone please help me understand this behavior of the DataGrid?

ouflak
  • 2,458
  • 10
  • 44
  • 49
Lucifer
  • 2,317
  • 9
  • 43
  • 67
  • 1
    Just to be sure, you are seeing an empty row at the bottom of your DataGrid, correct? What should happen is that if you enter something into this row, then a new row will automatically appear at the bottom of the DataGrid. Is this the behavior you are expecting and not seeing? Sorry if that seems an obvious question, but sometimes it is not obvious what people are expecting. – ouflak Mar 11 '14 at 16:42
  • Yes @ouflak, that's the behavior i'm expecting. But I don't get that new row automatically appearing at the bottom of the DataGrid. – Lucifer Mar 11 '14 at 17:03
  • 1
    What is the type of your ItemsSource? Type T must have parameterless constructor. I will give this a go when I'm back at my office. – ouflak Mar 11 '14 at 21:45
  • I missed the parameterless constructor. Thanks for pointing out on that. Though I am facing another weird issue with the DataGrid. Please refer the Update part of my question. – Lucifer Mar 12 '14 at 09:05
  • Have you updated yet? Not seeing it... Might be might refresh is not working.... – ouflak Mar 12 '14 at 09:17
  • @ouflak, I have updated the question. There was a little delay from my side ;) – Lucifer Mar 12 '14 at 09:53

3 Answers3

16

In my case,

First ensure your ItemSource is not using an array that can't add new item to it,

use something like List that can add newItem,

Besides, the SomeClass should have an default constructor takes no parameters like

List<SomeClass>();

public Class SomeClass
{       
  public SomeClass() { }
}

then the new empty row appear in the bottom of the DataGrid.

Refer to this answer.

Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
yu yang Jian
  • 6,680
  • 7
  • 55
  • 80
5

I'm going ahead and posting this as an answer here as I need to post a code sample and the comments are starting to become extended (got the invite-to-chat message).

The answer to the original question was ensure that the Type T of the ItemsSource had a parameterless constructor.

Try this code attached to the DataGrid's BeginningEdit event to swallow up the cell border clicks:

private void Grid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
{
    //// Have to do this in the unusual case where the border of the cell gets selected
    e.Cancel = true;
}

If you are actually using this handler for something else, or intend to, you can check the OriginalSource to see if it is a Border and cancel the event on that condition.

ouflak
  • 2,458
  • 10
  • 44
  • 49
  • That does solve the problem, but partially. I am still unable to add any further rows. – Lucifer Mar 12 '14 at 11:16
  • Ok so now, you can see the original empty row. You can edit it. But there is no additional row automatically created? Is that right? – ouflak Mar 12 '14 at 11:19
  • Is anybody able to reproduce the issue ? Or am I doing anything wrong ? – Lucifer Mar 12 '14 at 15:06
  • I have reproduced it, but I'm a bit tied up with a few things at the moment, and I'm frankly a bit stuck as to what further to do. I've got some cludgy ideas like handling the Cell's Selected event and adding a new 'blank' item into the ItemsSource or something along those lines. But something like that would really only be for testing. The built in functionality should work. *shrug* – ouflak Mar 12 '14 at 15:59
3

Use DataGridTextColumn and DataGridComboBoxColumn instead DataGridTemplateColumn, then rows will be added by adequately.

If you want to use DataGridTemplateColumn, then set not only the CellTemplate, but CellEditingTemplate. For example:

<DataGridTemplateColumn Header="Pick a Date">
<DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
        <TextBlock Text="{Binding myDate}" />
    </DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
    <DataTemplate>
        <DatePicker SelectedDate="{Binding myDate}" />
    </DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>

Mentor
  • 3,058
  • 1
  • 22
  • 27