Wednesday, August 22, 2007

DataGrid paging sorting, ASP.NET Development California

ASP.NET Development California
DataGrid paging sorting
1. Binding to the DataGrid: To
A. DataSet
1. DataTable (in a dataset)
2. DataView (sorted, filtered view of a DataTable)
B. DataReader: Database specific
2. AllowPaging:
A. Adds paging controls and limits to 10 rows per page
B. BUT… you still have to write code to do paging
3. AllowSorting:
A. Turns on links to column headers so you can click on them
B. You still have to write code to sort the data on the click…
C. You don’t sort the grid…you sort the data (dataview) and display in the grid.
4. AutoGenerateColumns:
A. Turns on/off the generation of columns automatically form the datasource
B. Nice but you loose control
5. Hint: This just adds tags to HTML…which you can do manually…
A. But properties are a great way to learn what you can do.
6. Autoformat: Right Click on Grid
A. Changes a number of properties…of which you can modify as needed.
1. Note the changes in the HTML View
7. Managing/Creating Columns: Property builder
a) Columns….uncheck Create auto columns
2. Bound columns: Link to a data source (ie data field/column)
3. Button columns…inserts buttons to trigger various events based upon the action selected(________Command) insert, update, edit, delete
4. HyperLink Column: creates hyperlink in column
5. Template: Lets you define what goes in the column…ie drop down list.
B. Data Formatting: uses “standard expressions”
1. Select Columns (not the format). Note the last box
2. {0:C} the 0 is the replaceable number and C is format to apply to it.
a) D = decimal, C = Currency
3. Hint: Check help for "Formatting Types"
C. Alignment: Format . Columns . Column . Items
D. Font Formatting…Use Format
8. Paging
A. _PageIndexChanged returns the e.NewPageIndex
1. grid.CurrentPageIndex = e.NewPageIndex says set the grid current page to the new page selected
2. Then rebind the grid
B. Property Builder . Paging to define the look of the paging..
9. Sorting:
A. AllowSorting = True (properties)
B. Columns must have a sort expression set in property builder
C. Clicking on header raises “SortCommand” event and e has your sort expression in it.
1. We use this to call the HandleSort sub to set sort and direction variables.
10. How to Sort
A. Key is dv.sort field, order
B. Set session variables to hold the SortColumn & SortDirection
C. Write a HandleSort sub routine to toggle the Sort direction and sort field.
1. Toggle direction.
D. Now we can use dv.sort property to modify the view
E. And rebind

11. Selecting a row: Clicking on the Select (or sort) Hyperlink
A. Raises the ItemCommand event
B. Check the e.CommandName to determine the type of link triggered it.
1. e.CommandName = “Select” means they clicked on a button/hyperlink with “select” in command property
C. Then grab the row from the dataset associated with the selected item
1. rebuild the dataset from a session variable in the same “sorted order”
2. Get the selected row with DataSetIndex property
3. Get the value of a field in that row using the field name.
Case "Select"
dv = CType(Session("DS"), DataSet).Tables(0).DefaultView
dv.Sort = Session("SortColumn").ToString & " " & Session("SortDirection")
drv = dv.Item(e.Item.DataSetIndex)
lblSelectedItem.Text = drv("ProductName").ToString & ": " & drv("UnitsInStock").ToString

Orange county california developers