ASP Gridview - Gridview in ASP.Net -GridView Example- How to use GridView in ASP.Net
The new data source controls in ASP.NET 2.0 make accessing data much quicker and easier than with ASP.NET 1.x. Similarly, the GridView makes displaying that data much simpler than with the DataGrid in ASP.NET 1.x. With the GridView you can specify that the data be sortable and/or pageable by simply checking a checkbox. Same for adding a Delete column or for allowing the user to edit the GridView's underlying data.
In this article we'll look at how to use the GridView to display data. To start, we'll take a look at the data source controls and see how to bind various types of data to a GridView, all without writing a single line of code. After these preliminary examples, we'll move on to examining how to accomplish a number of real-world tasks with the GridView, including: formatting; paging, sorting, editing, and deleting; filtering data; effectively displaying drill-down data; and other examples. Each example we'll look at includes a discussion on the new techniques examined and, where source code is needed, both Visual Basic and C# code is provided. Best of all, you can download the complete, working examples and try them out as-is or tweak them to meet your particular needs.
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>How to use GridView in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridViewExample" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="EmployeeID"
DataSourceID="SqlDataSourceData">
<Columns>
<asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" ReadOnly="True"
SortExpression="EmployeeID" />
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName"
SortExpression="CompanyName" />
<asp:BoundField DataField="PersonName" HeaderText="PersonName"
SortExpression="PersonName" />
<asp:BoundField DataField="PersonTitle" HeaderText="PersonTitle"
SortExpression="PersonTitle" />
<asp:BoundField DataField="Address" HeaderText="Address"
SortExpression="Address" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
<asp:BoundField DataField="Region" HeaderText="Region"
SortExpression="Region" />
<asp:BoundField DataField="PinCode" HeaderText="PinCode"
SortExpression="PinCode" />
<asp:BoundField DataField="Country" HeaderText="Country"
SortExpression="Country" />
<asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
<asp:BoundField DataField="Fax" HeaderText="Fax" SortExpression="Fax" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSourceData" runat="server"
ConnectionString="<%$ ConnectionStrings:AppSQLConnectionString %>"
SelectCommand="SELECT * FROM [Employee]"></asp:SqlDataSource>
</div>
</form>
</body>
</html>
There is another cool feature that I want to mention here. Suppose you have many records in the database and you execute a query and some of the records in the database are blank meaning that there is nothing inside them but just merely NULL values. When you bind those to the grid view control as expected you will get some blank rows on the page which really looks ugly. Grid view exposes a property named EmptyDataText which can be used to fill the rows with the text which are empty and hence return nothing. This will only work and the text will only be shown if EmptyDataTemplate is not defined.
I don't know if you have noticed or not but did you wonder that where are the edit,update and cancel buttons which were used for in-place editing feature. Well they all are placed under the CommandField type column.
You can add Edit, Delete, Select, Update and Cancel link buttons to your grid view control. In Asp.net 1.1 and using the datagrid control we had to write some code in order to get the datagrid in the edit mode. Grid View does not need any code to transform into edit mode and you can just switch to edit mode with a click of a button also you don't need any code in order to cancel the edit mode and switch the grid view back to its original condition.
The new data source controls in ASP.NET 2.0 make accessing data much quicker and easier than with ASP.NET 1.x. Similarly, the GridView makes displaying that data much simpler than with the DataGrid in ASP.NET 1.x. With the GridView you can specify that the data be sortable and/or pageable by simply checking a checkbox. Same for adding a Delete column or for allowing the user to edit the GridView's underlying data.
In this article we'll look at how to use the GridView to display data. To start, we'll take a look at the data source controls and see how to bind various types of data to a GridView, all without writing a single line of code. After these preliminary examples, we'll move on to examining how to accomplish a number of real-world tasks with the GridView, including: formatting; paging, sorting, editing, and deleting; filtering data; effectively displaying drill-down data; and other examples. Each example we'll look at includes a discussion on the new techniques examined and, where source code is needed, both Visual Basic and C# code is provided. Best of all, you can download the complete, working examples and try them out as-is or tweak them to meet your particular needs.
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>How to use GridView in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridViewExample" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="EmployeeID"
DataSourceID="SqlDataSourceData">
<Columns>
<asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" ReadOnly="True"
SortExpression="EmployeeID" />
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName"
SortExpression="CompanyName" />
<asp:BoundField DataField="PersonName" HeaderText="PersonName"
SortExpression="PersonName" />
<asp:BoundField DataField="PersonTitle" HeaderText="PersonTitle"
SortExpression="PersonTitle" />
<asp:BoundField DataField="Address" HeaderText="Address"
SortExpression="Address" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
<asp:BoundField DataField="Region" HeaderText="Region"
SortExpression="Region" />
<asp:BoundField DataField="PinCode" HeaderText="PinCode"
SortExpression="PinCode" />
<asp:BoundField DataField="Country" HeaderText="Country"
SortExpression="Country" />
<asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
<asp:BoundField DataField="Fax" HeaderText="Fax" SortExpression="Fax" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSourceData" runat="server"
ConnectionString="<%$ ConnectionStrings:AppSQLConnectionString %>"
SelectCommand="SELECT * FROM [Employee]"></asp:SqlDataSource>
</div>
</form>
</body>
</html>
There is another cool feature that I want to mention here. Suppose you have many records in the database and you execute a query and some of the records in the database are blank meaning that there is nothing inside them but just merely NULL values. When you bind those to the grid view control as expected you will get some blank rows on the page which really looks ugly. Grid view exposes a property named EmptyDataText which can be used to fill the rows with the text which are empty and hence return nothing. This will only work and the text will only be shown if EmptyDataTemplate is not defined.
I don't know if you have noticed or not but did you wonder that where are the edit,update and cancel buttons which were used for in-place editing feature. Well they all are placed under the CommandField type column.
You can add Edit, Delete, Select, Update and Cancel link buttons to your grid view control. In Asp.net 1.1 and using the datagrid control we had to write some code in order to get the datagrid in the edit mode. Grid View does not need any code to transform into edit mode and you can just switch to edit mode with a click of a button also you don't need any code in order to cancel the edit mode and switch the grid view back to its original condition.