1

Good day. How programmatically prevent the creation of new rows in data grid? For example:

private void SomeDataGridEvent()
   if (MyBoolConditionIsTrue())
      Prevent_Grid_Row_Creation_Logic
   else
     //Do Nothing. The new row is created

Edit1: Note: CanUserDeleteRows must be TRUE. The user CAN add rows, but only if some condition is true.

3

2 Answers 2

2

You can do it like that:

AddingNewItem="DataGrid_AddingNewItem"

private void DataGrid_AddingNewItem(object sender, AddingNewItemEventArgs e)
{
    if (sender is DataGrid dg)
    {
        if (MyBoolConditionIsTrue())
        {
            dg.Items.Remove(e.NewItem);
        }
        else
        {
            //Do Nothing. The new row is created
        }
    }
}

Or

RowEditEnding="DataGrid_RowEditEnding"

private void DataGrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
{
    if (MyBoolConditionIsTrue())
    {
        e.Cancel = true;
    }
    else
    {
        //Do Nothing. The new row is created
    }
}
1
  • The correct answer is "DataGrid_RowEditEnding". Thanks!
    – Freddy
    Commented Aug 13, 2020 at 8:24
0

Set CanUserAddRows property of the dataGrid to "False"

New contributor
AJRS is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.