2.17. Inserting a Row Within a GridView
You want to provide the ability for a user to insert a new row of data within a
GridView and an updateable data source, such as an
asp:SqlDataSource, to the .aspx file, set the
ShowFooter attribute of the
GridView control to
Footer-Template elements for each column that is to be inserted, add an Insert button in the footer, and initialize the properties of the data source in the code-behind. When the user clicks the Insert button, set the parameter values from the entered data and use the data source to insert the data in the
GridView's RowCommand event handler in the code-behind.
In the .aspx file:
GridViewcontrol where the data is to be displayed.
ShowFooterattribute of the
FooterTemplateelements for each column that is to be inserted.
Page_Init event handler of the code-behind class for the page, use the .NET language of your choice to:
SelectCommandproperty of the
SqlDataSourceto the SQL statement used to get the data to display from the database.
InsertCommandproperty of the
SqlDataSourceto the SQL statement (with parameters) used to insert in the database the data the user enters.
Add the parameters for the data the user can update to the
InsertParameterscollection of the
RowCommandevent handler, set the values of the
SqlDataSourcefrom the controls containing ...