Simple DataGrid Demo

This demo shows how to bind the results of a database query to a DataGrid using formatting properties and style information (specifically a HeaderStyle and AlternatingItemStyle).


FAQIDDescriptionViewCountFAQCategoryIDSubmittedByNameSubmittedByEmailDateEnteredCatName
181How can I format numbers and date/times using ASP.NET? For example, I want to format a number as a currency.87009922Scott Mitchellmitchell@4guysfromrolla.com1/19/2002 3:12:07 PMASP.NET
161How can I convert a Recordset into an array? Also, how can I convert an array into a Recordset?2140911Scott Mitchellmitchell@4guysfromrolla.com6/14/2001 5:02:02 PMArrays
115I 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?21372721Scott Mitchellmitchell@4guysfromrolla.com1/17/2001 11:38:49 PMDatabases, Errors
83How can I quickly sort a VBScript array? 2069331Richard Lowechadich@yahoo.com11/12/2000 1:58:41 AMArrays
106How can I find out if a record already exists in a database? If it doesn't, I want to add it.16379812Steve Ciminosteve_cimino@hotmail.com12/14/2000 4:59:11 PMDatabases, Queries
190How do I display data on a web page using arrays instead of Do...While...MoveNext...???...1628161Scott Mitchellmitchell@4guysfromrolla.com4/11/2002 1:14:41 PMArrays
118When 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... 1474784Bill Wilkinsonbill@ClearviewDesign.com1/22/2001 5:10:01 PMFileSystemObject
30For session variables to work, must the Web visitor have cookies enabled?1147686Scott Mitchellmitchell@4guysfromrolla.com9/26/2000 11:13:06 PMSession Object
14Can I send emails without using CDONTS?11183010Scott Mitchellmitchell@4guysfromrolla.com9/24/2000 1:18:37 AMEmail
153How 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?11045612Bill Wilkinsonbill@ClearviewDesign.com4/19/2001 3:15:34 PMDatabases, Queries


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)
    BindData()
  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
    myConnection.Open()
    dgPopularFAQs.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
    dgPopularFAQs.DataBind()	
  End Sub
</script>

	<asp:DataGrid runat="server" id="dgFAQs" 
	                BackColor="#eeeeee" Width="85%"
	                HorizontalAlign="Center"
	                Font-Name="Verdana"
	                Font-Size="10pt">
	  <HeaderStyle BackColor="Black" ForeColor="White" Font-Bold="True"
			 HorizontalAlign="Center" />
	  <AlternatingItemStyle BackColor="White" />
	</asp:datagrid>
	


[Return to the article]