www.jlion.com

Thursday, January 25, 2007

I often use the ASP.NET 2.0 DataGrid control. It's easy to bind to a datatable or arraylist, and the DataGrid supports paging.

There are a couple of tasks that I frequently need to perform with my DataGrids but that are non-intuitive enough to send me looking back through old projects for sample source code. They are:

Display an image in a column
I like to display images in columns to indicate status as well as a means for the user to execute some action (delete, zoom in, etc.). I've found that the control works well for this purpose, where the value assigned to the control's Text property is an IMG SRC tag and the value assigned to NavigateURL is either null ('') or the page that should be displayed.

Here's an example of the HyperLink control being used to display an Edit button:

<asp:templatecolumn>
<headerstyle width="25px"></headerstyle>
<itemstyle cssclass="CenterImage">
<itemtemplate>
<asp:HyperLink runat="server"
Text='<%#"<img src=" & chr(34) &amp;amp; " title=" & chr(34) & " alt=" & chr(34) & " border=" & chr(34) & " />"%>'
NavigateUrl='<%#"adCallDisp_E.aspx?ID=" & DataBinder.Eval(Container, "DataItem.ID") & "&A=E" %>'
ID="Hyperlink4">
</asp:HyperLink>
</itemtemplate>
</asp:TemplateColumn>
To embed code in the HyperLink control, use <%# and %> tags. One handy use for embedded code is to call a protected function in the page's code-behind that returns the name of the image to use based on some parameter or combination of parameters.

Values in DataGrid's datasource can be referenced using DataBinder.Eval(Container,"DataItem.fieldname")


Change the currently displayed page
Changing the current page in the DataGrid is quite effortless but may need to be optimized for large data sets (perhaps by passing the page id in so that the query only retrieves rows that will be displayed).

Protected Sub dgDisp_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles dgDisp.PageIndexChanged
dgDisp.CurrentPageIndex = e.NewPageIndex

Dim oFactory As New MutualSolicitationLib.Call_Disposition_Factory
dgDisp.DataSource = oFactory.GetAllDispositions
dgDisp.DataBind()
End Sub

Change the style of a row depending on the values of its columns
Finally, the DataGrid has a PreRender event that allows for customization of its appearence. Here's a bit of code that changes the style of rows representing items that have been disabled.

Protected Sub dgDisp_RowDataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgDisp.PreRender
Dim oDataGrid As DataGrid = CType(sender, DataGrid)
Dim oDataArray As ArrayList = CType(oDataGrid.DataSource, ArrayList)

If Not oDataArray Is Nothing Then
Dim iFirstRow = oDataGrid.PageSize * dgDisp.CurrentPageIndex

For i As Integer = iFirstRow To IIf(iFirstRow + oDataGrid.PageSize < oDataArray.Count - 1, iFirstRow + oDataGrid.PageSize, oDataArray.Count - 1)
Dim oDisp As MutualSolicitationLib.Call_Disposition = oDataArray(i)

If oDisp.IsActive = False Then
Dim oRow As DataGridItem = oDataGrid.Items(i - iFirstRow)
oRow.CssClass = "Disabled"
End If
Next
End If
End Sub

0 Comments:

Post a Comment

<< Home