A DataGrid Demo Using Templates

This demo illustrates how to use templates in a DataGrid Web control. Templates allow a much higher degree of HTML output customization than the default DataGrid output or the output from BoundColumns. While this demo may not be extremely pretty, it (hopefully) illustrates that the TemplateColumn allows for a high degree of HTML customization, much more so than the BoundColumn.

FAQ IDFAQ Information
Description: How can I format numbers and date/times using ASP.NET? For example, I want to format a number as a currency.
Category Name: ASP.NET
View Count: 964,702
Description: How can I convert a Recordset into an array? Also, how can I convert an array into a Recordset?
Category Name: Arrays
View Count: 238,089
Description: I am using Access and getting a 80004005 error (or a [Microsoft][ODBC Microsoft Access Driver] The Microsoft Jet database engine cannot open the file '(unknown)' error) when trying to open a connection! How can I fix this problem?
Category Name: Databases, Errors
View Count: 233,530
Description: How can I quickly sort a VBScript array?
Category Name: Arrays
View Count: 230,033
Description: How do I display data on a web page using arrays instead of Do...While...MoveNext...???...
Category Name: Arrays
View Count: 186,006
Description: How can I find out if a record already exists in a database? If it doesn't, I want to add it.
Category Name: Databases, Queries
View Count: 183,511
Description: When I get a list of all files in a directory via the FileSystemObject, they aren't ordered in any reasonable way. How can I sort the files by name? Or by size? Or by date created? Or...
Category Name: FileSystemObject
View Count: 168,750
Description: For session variables to work, must the Web visitor have cookies enabled?
Category Name: Session Object
View Count: 133,635
Description: Can I send emails without using CDONTS?
Category Name: Email
View Count: 130,322
Description: How can I remove multiple spaces between words in a string? That is, if I have: Hi    there how can I get: Hi there?
Category Name: Strings
View Count: 129,557

Source Code
<% @Import Namespace="System.Data" %>
<% @Import Namespace="System.Data.SqlClient" %>
<script language="vb" runat="server">
  Sub Page_Load(sender as Object, e as EventArgs)
  End Sub
  Sub BindData()
    '1. Create a connection
    Dim myConnection as New SqlConnection(ConfigurationSettings.AppSettings("connectionString"))

    '2. Create the command object, passing in the SQL string
    Const strSQL as String = "sp_Popularity"
    Dim myCommand as New SqlCommand(strSQL, myConnection)

    'Set the datagrid's datasource to the datareader and databind
    dgPopularFAQs.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
  End Sub

    <asp:datagrid id="dgPopularFAQs" runat="server"
	    <asp:BoundColumn DataField="FAQID" ItemStyle-Width="10%" 
				ItemStyle-HorizontalAlign="Center" HeaderText="FAQ ID" />
	    <asp:TemplateColumn HeaderText="FAQ Information">
	        <table border="0">
	            <td align="right"><b>Description:</b></td>
	            <td><%# DataBinder.Eval(Container.DataItem, "Description") %></td>
	            <td align="right"><b>Category Name:</b></td>
	            <td><%# DataBinder.Eval(Container.DataItem, "CatName") %></td>
	            <td align="right"><b>View Count:</b></td>
	            <td><%# DataBinder.Eval(Container.DataItem, "ViewCount", "{0:#,###}") %></td>

[Return to the article]