asp:HotTip
LANGUAGES: C#
TECHNOLOGIES: Forms Authentication
Make a Scrolling DataGrid
Create DataGrids that expose vast amounts of information - without using up tons of real estate.
By Jeff Prosise
You've seen pageable DataGrids and sortable DataGrids, but how about scrolling DataGrids? With a little help from a <div> tag, you can create scrolling DataGrids that expose vast amounts of information without consuming commensurate amounts of real estate. Copy the following code into an ASPX file to see how to use a DataGrid that displays the contents of the Northwind database's Products table:
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<body>
<form runat="server">
<div style="height: 256px; overflow: auto">
<asp:DataGrid ID="MyDataGrid" Width="100%"
RunAt="server" />
</div>
</form>
</body>
</html>
<script language="C#" runat="server">
void Page_Load (Object sender, EventArgs e)
{
if (!IsPostBack) {
SqlConnection connection = new SqlConnection
("server=localhost;database=northwind;uid=sa");
try {
connection.Open ();
SqlCommand command = new SqlCommand
("select * from products", connection);
SqlDataReader reader = command.ExecuteReader ();
MyDataGrid.DataSource = reader;
MyDataGrid.DataBind ();
}
finally {
connection.Close ();
}
}
}
</script>
Jeff Prosise is the author of several programming books, including Programming Microsoft .NET(Microsoft Press). He also is a co-founder of Wintellect (http://www.wintellect.com), a software consulting and education firm that specializes in .NET.