How to: Validate Data When a New Row is Added to a ListObject Control
Users can add new rows to a ListObject control that is bound to data. You can validate the user's data before committing the changes to the data source.
Applies to: The information in this topic applies to document-level projects and VSTO add-in projects for Excel. For more information, see Features Available by Office Application and Project Type.
Whenever a row is added to a ListObject that is bound to data, the BeforeAddDataBoundRow event is raised. You can handle this event to perform your data validation. For example, if your application requires that only employees between the ages of 18 and 65 can be added to the data source, you can verify that the age entered falls within that range before the row is added.
|
To validate data when a new row is added to data-bound ListObject
Create variables for the ID and DataTable at the class level.
Create a new DataTable and add sample columns and data in the
Startupevent handler of theSheet1class (in a document-level project) orThisAddInclass (in an VSTO Add-in project).Add code to the
list1_BeforeAddDataBoundRowevent handler to check whether the age entered falls within the acceptable range.
This code example assumes that you have an existing ListObject named list1 on the worksheet in which this code appears.
Extending Word Documents and Excel Workbooks in VSTO Add-ins at Run Time
Controls on Office Documents
Adding Controls to Office Documents at Run Time
ListObject Control
Automating Excel by Using Extended Objects
How to: Map ListObject Columns to Data