Skip to main content

70-562 GridView Part 1

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.

Comments

Popular posts from this blog

React JS Patterns

React JS is always evolving, and evolving quickly. These evolutions can be very significant ones, for example, the addition of hooks. React has a lot of code patterns, often these patterns are motivated by the DRY and/or the open-close principle. These patterns sometimes come in to replace a previous one, yet the previous ones still stays in use, as a consequence, the list of patterns keep growing. The goal of the next few posts will be to list the patterns commonly used in React JS developpement. Some patterns are more specific to JSX and I will start with these, and maybe add patterns specific to Redux.

Rxjs Forkjoin vs Zip

These Rxjs combination operators are handy to make Rest calls in parallel and combine their results. Both take n observables and will return the results, with the difference that forkJoin will complete even if one of the nested observables errors of completes.

Object.create vs. Object.Assign

The two functions return a new Object but with a difference. Object.assign will go through an enumerable and copy it's properties. Object.create will create a new empty object but link it's proto chain to the old object. One way to view this is with JSON.stringify(newCreatedObject) this would return an empty object, since all the properties are not part of the object's own properties, but inherited through prototype inheritance. In both case, the advantage is it allows to extended existing objects without modifying the original. This is particularly important when receiving arguments from a caller, in this case it's better to use these methods instead of modifying the caller's object since he might have planned to use it again later, expecting it to be in it's original state.