The GridView control is considered a successor to the DataGrid control. It is included wth VS2005 and is used with asp.web projects.
My first reaction is - it’s very tuned to be used with point and shoot wizards. Since I am a coding fool, that means I HATE IT! Actually that’s a first reaction, I like my code laid out in code rather than being disbursed between configuration files and html, so typically to use this control, I drop a SQL Datasource and a GRIDVIEW control onto a form. I might give the SQL Datasource a name like DS_GV and the Gridview a name like GV. Then I’ll hook them together via the DataSourceID property of the Grid View.
It does have an evolved feeling over the datagrid. On the otherhand you are more reliant on that datasource. Apparently the control can be used without the datasource but I wasn’t able to replicate the features of the DataGrid without it. It’s only real weakness, like the DataGrid - It doesn’t have a clean ‘ADD’ mechanism.
Here’s a sample of the code I use to set sql commands to the grid.
1 | ' |
As you can see, I’ve placed code to retrieve records and code to delete records into the datasource. I set a flag to allow deletions, and this was pretty much all the code that was needed to support this.
An alternative usage. Similar to the way the datagrid was used.
1 | sql = "select * " _ |
Actually, I place the datasource initialization logic into the page_load event.
Notice the delete command. The SQL has a parameter name starting with @. Parameter names are column names that are passed into the dataset. I ?dont? think you can use hidden columns. (The exception is keys)
I have the GV.Databind method hooked in at various locations in the code like after the Add method.
Events
The RowCommand is triggered if you have pressed a button on that row. Look at e.CommandName to find out which button was pressed.