Tips and Tricks For The Datagrid Control: - Developer
Tips and Tricks For The Datagrid Control: - Developer
AWF202
Tips and Tricks for the DataGrid
Control
Paul Litwin
Deep Training &
Fred Hutchinson Cancer Research Center
Paul Litwin
• Developer
? Focus: ASP.NET, ASP, VB, C#, SQL Server, …
? MCSD
? Microsoft MVP
? Lead Programmer with Fred Hutchinson Cancer Research Center
• Co-Founder and Senior Trainer
? Deep Training
• www.deeptraining.com
• Conference Chair/Speaker
? Chair, Microsoft ASP.NET Connections
? Member INETA Speakers Bureau
• Author
? Author/co-author of a dozen books, including …
• ASP.NET for Developers
• Access Cookbook, 2nd edition
• Access 2002 Desktop/Enterprise Dev Handbook
Session Objectives
• Explore various DataGrid properties and
events
• Learn how to sort columns in a grid in both
ascending and descending order
• Learn how to use automatic and custom
paging
• Explore editing, inserting, and deleting of grid
rows data in-place and push those updates
back to the database
• Learn how to use templated columns
www.DevConnections.com
ASP.NET & VS Connections May 6-9, 2003
Agenda
• DataGrid Basics
• Sorting it Out
• Pagination
• Updating Data using a DataGrid
• Template Columns
DataGrid Basics
DataGrid Control
• Columnar grid
• Supports sorting, paging & in-place
editing
www.DevConnections.com
ASP.NET & VS Connections May 6-9, 2003
DataGrid Control
• AutogenerateColumns Attribute of DataGrid
? True (default) – all DataSource columns included
as <asp:BoundColumn> columns
? False – you specify the columns
• Column Types
? BoundColumn – label/textbox
? ButtonColumn – button
? EditCommandColumn – button with special editing
capabilities
? HyperlinkColumn – linkbutton
? TemplateColumn – if none of the others apply; you
will have to do binding using <%# xxx %> syntax
Sub BindDataGrid( )
Dim strCnx As String = "server=localhost;uid =sa;pwd=;database=northwind ;"
Dim strSQL As String = _
"SELECT CustomerID, CompanyName , ContactName, " & _
"Country FROM Customers ORDER BY CompanyName"
www.DevConnections.com
ASP.NET & VS Connections May 6-9, 2003
Sorting it Out
Sorting Grids
• Steps to provide sorting
1. AllowSorting = “True”
2. Provide sorting event handler that retrieves
sort expression (name of sort column)
3. Sort data based on sort expression
...
www.DevConnections.com
ASP.NET & VS Connections May 6-9, 2003
Ascending/Descending Sorts
? Need to store away sort column and
direction between postbacks
? Use ViewState
www.DevConnections.com
ASP.NET & VS Connections May 6-9, 2003
CType(Session("dvCustomer"), DataView).Sort = _
ViewState("SortCol") & " " & ViewState("SortDir")
Call BindDataGrid()
End Sub
Pagination
Pagination
• Properties related to pagination
? AllowPaging
? AllowCustomPaging
? PageSize
? PagerStyle- Mode (NextPrev or NumericPages )
? PagerStyle- Position (Top, Bottom, TopAndBottom )
? PagerStyle- PageButtonCount
? …
www.DevConnections.com
ASP.NET & VS Connections May 6-9, 2003
Automatic Pagination
• AllowPaging = "True"
• You provide DataGrid with data source and a
PageSize and it takes care of chopping up data
into pages and displaying the correct page
• Very little coding on your part
• Requires query to be re-run for every page
unless you cache the data source somewhere
Pagination Examples
CustomersPageAuto1.aspx – rebuilds DataReader each time
CustomersPageAuto2.aspx – caches DataSet in Session
Custom Pagination
• AllowPaging = "True"
• AllowCustomPaging = "True"
• You provide DataGrid with each page of data
• More coding than automatic paging
www.DevConnections.com
ASP.NET & VS Connections May 6-9, 2003
Return sdr
End Function
www.DevConnections.com
ASP.NET & VS Connections May 6-9, 2003
Return sdr
End Function
www.DevConnections.com
ASP.NET & VS Connections May 6-9, 2003
www.DevConnections.com
ASP.NET & VS Connections May 6-9, 2003
www.DevConnections.com
ASP.NET & VS Connections May 6-9, 2003
' Turn on constraints that may have been disabled for insert
ds.EnforceConstraints = True
www.DevConnections.com
ASP.NET & VS Connections May 6-9, 2003
Template Columns
<asp:TemplateColumn HeaderText="Customer">
<itemtemplate>
<asp:Label
Text='<%# CreateLinkCol(Container.DataItem("CustomerId "),
Container.DataItem("CompanyName ")) %>'
runat="server"/>
</ItemTemplate>
</asp:TemplateColumn>
www.DevConnections.com
ASP.NET & VS Connections May 6-9, 2003
DropDownList Example
DataGridWithDropDown.aspx– HTML
<asp:templatecolumn headertext ="Title">
<itemtemplate>
<%# Container.DataItem("Title" ) % >
</itemtemplate>
<edititemtemplate>
<asp:dropdownlist
id="drpTitle"
datatextfield="Title"
datavaluefield="Title"
datasource="<%#GetTitlesDataSource()%>"
selectedindex='<%#GetTitlesSelectedIndex(Container.DataItem("Title"))%>'
runat="server"/>
</edititemtemplate>
</asp:templatecolumn>
DropDownList Example
DataGridWithDropDown.aspx– GetTitlesDataSource function
www.DevConnections.com
ASP.NET & VS Connections May 6-9, 2003
DropDownList Example
DataGridWithDropDown.aspx– GetTitlesSelectedIndex function
Public Function GetTitlesSelectedIndex(ByVal objItem As Object) _
As Integer
'Provide Initial SelectedIndex to Title column's EditItemTemplate
Dim intI = 0
Dim dtEmpTitles As DataTable = ds.Tables("EmployeeTitles")
Dim drEmpTitles As DataRow
' Handle null values
If objItem Is System.DBNull.Value Then
objItem = "(none)"
End If
' Iterate through Title rows to locate matching item.
' When a match is found, intI will be SelectedIndex.
For Each drEmpTitles In dtEmpTitles.Rows
If objItem = drEmpTitles("Title") Then
Return intI
End If
intI = intI + 1
Next
End Function
Summary
• We looked at how to sort columns in a grid in
both ascending and descending order
• We learned how to use automatic pagination
• We learned how to employ custom
pagination, both with arbitrarily-divided and
data-divided pages
• We looked at editing, inserting, and deleting
of grid rows data in-place
• Learned how to use templated columns to
provide encoding of querystrings and editable
dropdownlist controls
Thank You!
www.DevConnections.com