0
votes

I have a datagrid,in which I want to disable new rows being created at the bottom, when the user clicks on any cell on the datagrid. I only want the default behaviour,add blank row at bottom when the enter key is pressed.

Here is my Datagrid :

 <custom:CustomDataGrid ItemsSource="{Binding Items}"
                           AutoGenerateColumns="False"
                           RowHeight="27"
                           Grid.Row="1"
                           SelectionUnit="CellOrRowHeader"
                           RowHeaderWidth="30"
                           CanUserAddRows="true">
        <custom:CustomDataGrid.Columns>
            <custom:CustomDataGridTextColumn  Header="Product Name"
                                              Binding="{Binding ProductName,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                                              Width="250" />
            <custom:CustomDataGridTextColumn  Header="Unit Price"
                                              Binding="{Binding UnitPrice,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                                              Width="130" />
            <custom:CustomDataGridTextColumn  Header="Qty"
                                              Binding="{Binding Quantity,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                                              Width="130" />
            <custom:CustomDataGridTextColumn  Header="Amount"
                                              Binding="{Binding Amount,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                                              Width="130"
                                              IsLastEditableColumn="True" />
        </custom:CustomDataGrid.Columns>
    </custom:CustomDataGrid>

How do I prevent blank row being added to the datagrid when the user select other cells on the datagrid

2
You set datagrid prop CanUserAddRows="true", maybe you should try CanUserAddRows="False"Den
@Den ,If I set the CanUserAddRows="False" ,I cant add blank row when enter key is pressAngel

2 Answers

2
votes

To prevent the blank row being added you can set the property CanUserAddRows on the DataGrid to false.

To still be able to add rows you can handle one of the key events (eg KeyDown etc) to add an empty/default product to Items and set focus to the first editable column in the new item when the Enter key is pressed.

1
votes

Try setting IsReadOnly="True" in the DataGrid XAML.