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
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.
In the Properties window, set the AllowUserToAddItems property to False.
Set the AllowUserToDeleteItems property to False.
In the Windows Forms Designer, select the BindingNavigator control, and then click the AddNewItem button (the button with a plus sign on it).
In the Properties window, set the Enabled property to False.
In the Windows Forms Designer, select the BindingNavigator control, and then click the DeleteItem button (the button with a red X on it).
In the Properties window, set the Enabled property to False.
In the Component Tray, select the BindingSource to which the DataRepeater is bound.
In the Properties window, set the AllowNew property to False.
In the Windows Forms Designer, double-click the DeleteItem button to open the Code Editor.
In the Events drop-down list, select the BindingNavigatorDeleteItem_EnabledChanged event.
Add the following code to the BindingNavigatorDeleteItem_EnabledChanged event handler:
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
In the Windows Forms Designer, double-click the form to open the Code Editor.
Add the following code to the Form_Load event:
dataRepeater1.AllowUserToAddItems = false; dataRepeater1.AllowUserToDeleteItems = false; bindingNavigatorAddNewItem.Enabled = false; ordersBindingSource.AllowNew = false; bindingNavigatorDeleteItem.Enabled = false;
Add the following code to the BindingNavigatorDeleteItem_EnabledChanged event handler:
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)
Concepts
Introduction to the DataRepeater Control (Visual Studio)
Reference
Change History
Date |
History |
Reason |
---|---|---|
July 2008 |
Added topic. |
SP1 feature change. |