Learn How to Use the ASP.NET 4.0 GridView Control
In this tutorial we will learn how to create and Setup Gridview Controls. Gridviews can be helpful when listing information out of SQL Databases. The difference between the Gridview and the Repeater is the formatting, Gridviews generally look like Tables while Repeaters have a more free form style. More information on Repeaters go (link here).
We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.
Step 1. Setting up the Project and Creating the Gridview
1. Create a new Project in Visual Web Developer
2. Create a new Web From named “Default.aspx”
3. Create a new SQL Database and name it “Gridview.mdf”
4. Inside that Database create a Table called “Gridview” with the Columns ID(Primary Key, Identity), and Name(varchar(50))
5. Fill the Table out with some sample Data by right clicking it and selecting Show Table Data
6. Now go onto the form and drag an SQL data source object onto the form
7. Setup the SQL data source to connect to the Database and Table we just created by clicking on the Smart Tag and selecting Configure DataSource
I just signed up at Server Intellect and couldn’t be more pleased with my fully scalable & redundant cloud hosting! Check it out and see for yourself.
Step 2. Setting up the Gridview
1. Drag a Gridview Control from the Data Section of the Toolbar onto the Form
2. Click on the Smart Tag and select the Data Source we just created (“SqlDataSource1″ for me)
3. Now you have linked the Data Source to the Gridview you can now run the project and view the Gridview object in action.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [Gridview]"></asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
We used over 10 web hosting companies before we found Server Intellect. Our new server with cloud hosting,was set up in less than 24 hours. We were able to confirm our order over the phone. They responded to our inquiries within an hour. Server Intellect’s customer support and assistance are the best we’ve ever experienced.
Conclusion
In this tutorial we learned how to use the Gridview control in ASP.NET. The Gridview Control can be very useful for sorting information out of an SQL database into a table. The Gridview is also a simple more styled alternative to the Repeater Control information (here). Take your pick on which Control suits your website.
Gridview.zip
