Displaying Random Data

This live demo illustrates how to display five random records from the Articles table.


TitleDate WrittenCommentsViews
MouseOver Coloring for a DataGrid12/17/2002This article, by Colt Kwong, demonstrates how to create a DataGrid so that whenever you move the mouse over a DataGrid row, the row's background color changes.15666
Adding a DropDownList Web Control to the DataGrid's Editing Interface8/7/2002If you provide editing capabilities in your DataGrid for a database table with foreign keys, when allowing the user to edit the content you'll likely want to display the legal choices for the foreign key(s) in a DropDownList. This article, by Scott Mitchell and Matthew Rouse examines how to accomplish exactly this!6760
Bidirectional Sorting with Up and Down Arrows in the Header6/15/2005This article shows how to create a bi-directional sortable DataGrid that displays an up or down arrow in the header of the column that the grid is sorted by, depending on if the data is sorted in ascending or descending order.9796
Creating Custom Columns for the ASP.NET Datagrid9/10/2003This article, by DataGridGirl herself, examines how to build a custom DataGrid column that displays a databound DropDownList.13925
Advanced DataGrid Usage11/11/2002This PowerPoint presentation from Microsoft shows how to use the DataGrid Web control to accomplish a number of advanced tasks, such as Master/Detail DataGrids, summary DataGrids, and so on.8771


Source Code

<%@ Page Language="VB" %>
<script runat="server">

    Sub Page_Load(sender as Object, e as EventArgs)
      If Not Page.IsPostBack then
        dgArticles.DataSource = GetArticles()
        dgArticles.DataBind()
      End If
    End Sub
    
    
     Function GetArticles() As System.Data.SqlClient.SqlDataReader
         Dim connectionString As String = "connection string"
         Dim sqlConnection As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection(connectionString)
    
         Dim queryString As String = "SELECT TOP 5 ArticleID, [Articles].[Title], [Articles].[URL], [Articles].[dateAuthored], [Articles"& _
"].[Comments], [Articles].[ClickThroughs] FROM [Articles] ORDER BY NEWID()"
         Dim sqlCommand As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
    
         sqlConnection.Open
         Dim dataReader As System.Data.SqlClient.SqlDataReader = sqlCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
    
         Return dataReader
     End Function    
    
         Sub dgArticles_Sort(sender as Object, e As DataGridSortCommandEventArgs)
           dgArticles.DataSource = GetArticles(e.SortExpression)
           dgArticles.DataBind()
         End Sub

</script>

    <form runat="server">
        <p>
            <asp:DataGrid id="dgArticles" runat="server" AllowSorting="True" AutoGenerateColumns="False" Font-Size="10pt" Font-Names="Verdana" OnSortCommand="dgArticles_Sort">
                <HeaderStyle font-size="13pt" font-bold="True" horizontalalign="Center" forecolor="White" backcolor="#006699"></HeaderStyle>
                <AlternatingItemStyle backcolor="#EEEEEE"></AlternatingItemStyle>
                <Columns>
                    <asp:HyperLinkColumn DataNavigateUrlField="ArticleID" DataNavigateUrlFormatString="/Articles/Goto.aspx?ID={0}" DataTextField="Title" SortExpression="Title" HeaderText="Title"></asp:HyperLinkColumn>
                    <asp:BoundColumn DataField="DateAuthored" SortExpression="dateAuthored DESC" HeaderText="Date Written" DataFormatString="{0:d}"></asp:BoundColumn>
                    <asp:BoundColumn DataField="Comments" HeaderText="Comments"></asp:BoundColumn>
                    <asp:BoundColumn DataField="ClickThroughs" SortExpression="ClickThroughs DESC" HeaderText="Views" DataFormatString="{0:d}" ItemStyle-HorizontalAlign="Center"></asp:BoundColumn>
                </Columns>
            </asp:DataGrid>            
        </p>
    </form>
    

[Return to the FAQ...]