I will be adding code samples for these, this is notes I'm taking for the 70-762 exam, GridView chapter part 1.
GRIDVIEW OVERVIEW
The GridView replaces the 2.0 DataGrid, datagrid had too much configuration for sorting, grouping etc, that is now streamlined.
You can drag a table from the server explorer on to a page and it will add a gridview and sqldatasource automatically, or you cando the same thing by selecting just the fields you need.
Cick the gridview smartag to see gridview tasks like formating, but the autoformats are horrible. It can all be styled with some patience but thats another topic.
Creating a basic GirdView can be done entirely by drag n drop, followed by drag and drop a datasource, enable typical operations(sorting, select, edit) from the smarttag of the grid, in design mode
Typically those two objects form a two-tier application(presentation+data access layer). For n-tier, a middle layer(data repository) is added mapping datatables to objects, typically Linq, Entity or NHibernate are used to add this data to object model.
Adding through the designer adds markup(and also code behind) but all properties added from the designer are reflected in the markup(aspx page).
For exmaple the DataSourceID says which datasource the grid is bound to.
FILTERING, AND COMBINING TWO 'COMPLEX CONTROLS' (CONTROLS THAT DISPLAY MORE THAN ONE DATA ROW)
For filtering the grid, the datasource is modified, for example adding a DropDownList bound to a different DataSource than the gridview.
This DataSource of the ddl is configured to provide unique rows of single collumn of the selected table.
Remeber to add room in your form to put the ddl above the grid, just place the cursor and hit enter, this will create a line break.
For the ddl, check the box to enable auto postback.
Tell the grid to filter itself by configuring sql DataSource of the GridView based on the value in the ddl, by adding a where clause that points to that value, by clicking the datasource in the designer and configure datasource, you can test this query within the designer. All this with no code written, didn't even need to write the SelectedIndexChanged of the ddl.
ADDING THE DDL INSIDE THE GRIDVIEW
What if editing a row should be done from a list of existing values rather than a textbox? The you need to need to edit the field to be a template field.
Any BoundField of the grid view that you can view in the source can be converted into a template field. You can use the ItemTemplate or AlternatingItem Template of the GridView, and you can add TemplateFields for the header of footer also. Simple just convert the column to a templated colum.
In the smartTag of the grid, you select the edit column link, in the Selected Fields list, select the column you want to turn into a Template field and click the link convert to TemplateFiled, it has a different icon in the list of fields after being modified to template fileds. In the list above Available fields you can pick any of the available fields.(CheckboxField, HyperLinkField, ImageFiled, ButtonField, CommandField).
Now back in the grid(in the opening menu of SmartTag, click Edit Template. In gridview task drop down, you select the column you want to modify, for example the default template is an textbox, if you want a ddl, delete that textbox and drag in the ddl. You need to decide if you want this to cause a postback.
Like any ddl, the first you need to do is select it's DataSource, the for the SelectedValue property, you need to set the field binding to the collumn of your choice for this ddl. To persist the selected value to the DataSet
you need to check the checkbox Two-way databinding. You can put a string in the format text box to provide the typical string you'd provide in string.Format().
After you're done configuring the TemplatedField, go back to the SmartTag of the GridView and click end template editing to go back in the normal design view. Save and browse and test that it works.
GRIDVIEW OVERVIEW
The GridView replaces the 2.0 DataGrid, datagrid had too much configuration for sorting, grouping etc, that is now streamlined.
You can drag a table from the server explorer on to a page and it will add a gridview and sqldatasource automatically, or you cando the same thing by selecting just the fields you need.
Cick the gridview smartag to see gridview tasks like formating, but the autoformats are horrible. It can all be styled with some patience but thats another topic.
Creating a basic GirdView can be done entirely by drag n drop, followed by drag and drop a datasource, enable typical operations(sorting, select, edit) from the smarttag of the grid, in design mode
Typically those two objects form a two-tier application(presentation+data access layer). For n-tier, a middle layer(data repository) is added mapping datatables to objects, typically Linq, Entity or NHibernate are used to add this data to object model.
Adding through the designer adds markup(and also code behind) but all properties added from the designer are reflected in the markup(aspx page).
For exmaple the DataSourceID says which datasource the grid is bound to.
FILTERING, AND COMBINING TWO 'COMPLEX CONTROLS' (CONTROLS THAT DISPLAY MORE THAN ONE DATA ROW)
For filtering the grid, the datasource is modified, for example adding a DropDownList bound to a different DataSource than the gridview.
This DataSource of the ddl is configured to provide unique rows of single collumn of the selected table.
Remeber to add room in your form to put the ddl above the grid, just place the cursor and hit enter, this will create a line break.
For the ddl, check the box to enable auto postback.
Tell the grid to filter itself by configuring sql DataSource of the GridView based on the value in the ddl, by adding a where clause that points to that value, by clicking the datasource in the designer and configure datasource, you can test this query within the designer. All this with no code written, didn't even need to write the SelectedIndexChanged of the ddl.
ADDING THE DDL INSIDE THE GRIDVIEW
What if editing a row should be done from a list of existing values rather than a textbox? The you need to need to edit the field to be a template field.
Any BoundField of the grid view that you can view in the source can be converted into a template field. You can use the ItemTemplate or AlternatingItem Template of the GridView, and you can add TemplateFields for the header of footer also. Simple just convert the column to a templated colum.
In the smartTag of the grid, you select the edit column link, in the Selected Fields list, select the column you want to turn into a Template field and click the link convert to TemplateFiled, it has a different icon in the list of fields after being modified to template fileds. In the list above Available fields you can pick any of the available fields.(CheckboxField, HyperLinkField, ImageFiled, ButtonField, CommandField).
Now back in the grid(in the opening menu of SmartTag, click Edit Template. In gridview task drop down, you select the column you want to modify, for example the default template is an textbox, if you want a ddl, delete that textbox and drag in the ddl. You need to decide if you want this to cause a postback.
Like any ddl, the first you need to do is select it's DataSource, the for the SelectedValue property, you need to set the field binding to the collumn of your choice for this ddl. To persist the selected value to the DataSet
you need to check the checkbox Two-way databinding. You can put a string in the format text box to provide the typical string you'd provide in string.Format().
After you're done configuring the TemplatedField, go back to the SmartTag of the GridView and click end template editing to go back in the normal design view. Save and browse and test that it works.
Comments
Post a Comment