Demo of a Scrollable DataGrid

This DataGrid's contents are scrollable, since the height is set to a hardcoded 300px.


 FAQ IDQuestionViews
View FAQ181How can I format numbers and date/times using ASP.NET? For example, I want to format a number as a currency.964,702
View FAQ161How can I convert a Recordset into an array? Also, how can I convert an array into a Recordset?238,089
View FAQ115I 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?233,530
View FAQ83How can I quickly sort a VBScript array? 230,033
View FAQ190How do I display data on a web page using arrays instead of Do...While...MoveNext...???...186,006
View FAQ106How can I find out if a record already exists in a database? If it doesn't, I want to add it.183,511
View FAQ118When 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... 168,750
View FAQ30For session variables to work, must the Web visitor have cookies enabled?133,635
View FAQ14Can I send emails without using CDONTS?130,322
View FAQ149How can I remove multiple spaces between words in a string? That is, if I have: Hi    there how can I get: Hi there?129,557
View FAQ153How can I take the result of a SELECT...MULTIPLE or a group of same-named checkboxes and turn it into a query? That is, if the user selects 3 answers, how can I construct a query that looks for all 3?128,350
View FAQ163How do I convert a string into an Integer?123,762
View FAQ107** UPDATED 24 June 2003 **: Why am I getting "Operation must use an updateable query" errors?119,416
View FAQ180How can I install Personal Web Server (PWS) onto Windows XP Home edition?116,884
View FAQ47How can I validate an email address using VBscript?115,252
View FAQ13How do you send email attachments through an ASP page?111,649
View FAQ172How can I display images that reside in my database through an ASP page?110,298
View FAQ63How do I put an array into a Session or Application "variable"? How do I then use it on the next ASP page?108,962
View FAQ89How can I sort a VB Script array in ways other than by case-sensitive alphanumeric order, like numeric value, length of string, or even randomly?105,385
View FAQ142How can I use application-level variables to cache information?97,894
View FAQ79**UPDATED 1 April 2002** How do I get the record number of a just added record? 97,514
View FAQ29How can I determine if a visitor has cookies support enabled in his/her browser?94,329
View FAQ38How can I dynamically change the action of a form?92,236
View FAQ160Why do I get the error "The application is using arguments that are of the wrong type, are out of acceptable range, or are in conflict with one another" when I try to open a recordset?91,729
View FAQ61How can I treat some other web page as data on my own site?91,306


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)
    If Not Page.IsPostBack then
		BindData()
	End If
  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 = "SELECT TOP 25 FAQID, Description, ViewCount FROM tblFAQ ORDER BY ViewCount DESC"
    Dim myCommand as New SqlCommand(strSQL, myConnection)

    'Set the datagrid's datasource to the datareader and databind
    myConnection.Open()
    dgPopularFAQs.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
    dgPopularFAQs.DataBind()	
    myConnection.Close()
  End Sub
</script>

<div style="height:300px; width:100%; overflow:auto;">
    <asp:DataGrid runat="server" id="dgPopularFAQs"
         AutoGenerateColumns="False"
         Font-Name="Verdana" Width="80%"
         Font-Size="11pt" HorizontalAlign="Center">
         
    <HeaderStyle BackColor="Navy" ForeColor="White" HorizontalAlign="Center"
                 Font-Size="14pt" Font-Bold="True" />
   
    <Columns>
      <asp:BoundColumn DataField="FAQID" HeaderText="FAQ ID" 
                ItemStyle-HorizontalAlign="Center" />
      <asp:BoundColumn DataField="Description" HeaderText="Question" />
      <asp:BoundColumn DataField="ViewCount" HeaderText="Views"
                DataFormatString="{0:#,###}"
                ItemStyle-HorizontalAlign="Right" />
    </Columns>
  </asp:DataGrid>
</div>	


[Return to the article]