Bagikan melalui


How to: Disable Adding and Deleting DataRepeater Items (Visual Studio)

By default, users can add and delete items in a DataRepeater control. Users can add a new item by pressing CTRL+N when a DataRepeaterItem has focus or by clicking the AddNewItem button on the BindingNavigator control. Users can delete an item by pressing DELETE when a DataRepeaterItem has focus or by clicking the DeleteItem button on the BindingNavigator control.

You can disable adding and/or deleting at design time or at run time.

To disable adding and deleting at design time

  1. In the Windows Forms Designer, select the DataRepeater control.

    Note

    You must select the lower section of the control. If you select the item template section, a different set of properties will be displayed.

  2. In the Properties window, set the AllowUserToAddItems property to False.

  3. Set the AllowUserToDeleteItems property to False.

  4. In the Windows Forms Designer, select the BindingNavigator control, and then click the AddNewItem button (the button with a plus sign on it).

  5. In the Properties window, set the Enabled property to False.

  6. In the Windows Forms Designer, select the BindingNavigator control, and then click the DeleteItem button (the button with a red X on it).

  7. In the Properties window, set the Enabled property to False.

  8. In the Component Tray, select the BindingSource to which the DataRepeater is bound.

  9. In the Properties window, set the AllowNew property to False.

  10. In the Windows Forms Designer, double-click the DeleteItem button to open the Code Editor.

  11. In the Events drop-down list, select the BindingNavigatorDeleteItem_EnabledChanged event.

  12. Add the following code to the BindingNavigatorDeleteItem_EnabledChanged event handler:

    If BindingNavigatorDeleteItem.Enabled = True Then
        BindingNavigatorDeleteItem.Enabled = False
    End If
    
    if (bindingNavigatorDeleteItem.Enabled == true)
    {
        bindingNavigatorDeleteItem.Enabled = false;
    }
    

    Note

    This step is necessary because the BindingSource will enable the DeleteItem button every time that the current record changes.

To disable adding and deleting at run time

  1. In the Windows Forms Designer, double-click the form to open the Code Editor.

  2. Add the following code to the Form_Load event:

    DataRepeater1.AllowUserToAddItems = False
    DataRepeater1.AllowUserToDeleteItems = False
    BindingNavigatorAddNewItem.Enabled = False
    ordersBindingSource.AllowNew = False
    BindingNavigatorDeleteItem.Enabled = False
    
    dataRepeater1.AllowUserToAddItems = false;
    dataRepeater1.AllowUserToDeleteItems = false;
    bindingNavigatorAddNewItem.Enabled = false;
    ordersBindingSource.AllowNew = false;
    bindingNavigatorDeleteItem.Enabled = false;
    
  3. Add the following code to the BindingNavigatorDeleteItem_EnabledChanged event handler:

    If BindingNavigatorDeleteItem.Enabled = True Then
        BindingNavigatorDeleteItem.Enabled = False
    End If
    
    if (bindingNavigatorDeleteItem.Enabled == true)
    {
        bindingNavigatorDeleteItem.Enabled = false;
    }
    

    Note

    This step is necessary because the BindingSource will enable the DeleteItem button every time that the current record changes.

See Also

Tasks

Troubleshooting the DataRepeater Control (Visual Studio)

Reference

DataRepeater

Concepts

Introduction to the DataRepeater Control (Visual Studio)