| Data Binding in ASP.Net 2.0 |
| |
| The GridView Web Server Control |
| |
| The GridView control is the enhanced version of DataGrid in ASP.NET 2.0. DataGrid required a page developer to write code to perform simple operations such as paging, sorting, editing or deleting data, the GridView control can automatically handle these operations provided its bound data source control supports these capabilities. |
| |
| The GridView control is as easier to use as similar to any normal control. You set up a data source object, provide the connection string and query text, and assign the ID of the data source to the DataSourceId property of the GridView. At run time, the GridView automatically binds to the source and generates appropriate columns of data. |
| |
| Displaying Data in GridView |
| |
| By default, all the columns in the query are displayed in the grid. To display just configure a datasource control with GridView and add a required query in SelectCommand attribute. |
| |
<%@ Page Language="VB" %>
<html>
<head>
<title>The GridView</title>
</head>
<body>
<form runat="server">
<asp:SqlDataSource ID="mySqlDataSource" runat="server"
ConnectionString=" ConnectionString to Database"
SelectCommand="Select Query String" />
<asp:GridView ID="myGridView" DataSourceID="mySqlDataSource" runat="server" />
</form>
</body>
</html> |
| |
| Sorting and Paging in GridView |
| |
| The GridView makes sorting and Paging so simple that it never before. Now it just like a childish work for a developer. You set the AllowPaging and AllowSorting property to True to enable the paging and sorting respectively. |
| |
<asp:GridView ID="myGridView" DataSourceID="mySqlDataSource" runat="server"
AllowPaging = "True"
AllowSorting = "True" /> |
| |
| After enabling sorting, you will find that all the grid columns have turns to hyperlinks. Clicking a column header sorts that specific column. The GridView can handle both ascending and descending sorting. A Sort method of GridView can also accept multiple SortExpressions to enable multicolumn sorting. |
| |
| Enabling the paging in the GridView control defaults to a page size of 10(ten) records and adds the Pager to bottom of the grid. The GridView allows the most of the paging options to be personalized. By specifying the PagerStyle element in the GridView, you can customize how the grid displays the Pager text, including the font color, size, and type, as well as text alignment and a variety of other style options. |
| |
| Updating Data in GridView |
| |
| You can perform edit in GridView in two way, both are equally simple. Using first method, modify the existing Data Source control by adding an UpdateCommand attribute. There's no need to work with ADO.NET or worry about commands or connections. To persist changes when the user clicks Update, write code like this: |
| |
<asp:sqldatasource runat="server" id="MySource"
connectionstring="ConnectionString"
updatecommand="Update Query String">
</asp:sqldatasource>
<asp:gridview runat="server" id="MyGridView"
DataSourceId="MySource"
DataKeyNames="employeeid" AutoGenerateEditButton="true">
</asp:gridview> |
| |
| In other Way you can simply set the AutoGenerateEditButton property of the GridView to True. |
| |
| For a GridView control, editing data means in-place editing and record deletion. As mentioned, in-place editing refers to the grid's ability to support changes to the currently displayed records. To enable in-place editing on a GridView, you turn on the AutoGenerateEditButton property. |
| |
| Deleting Data in GridView |
| |
| Deleting data in GridView Control is almost similar to editing, but it requires certain extra steps. First, you must set the AutoGenerateDeleteButton property to True. You will immediately notice a link titled Delete on the left side of the GridView control. You will then have to set the DeleteQuery property of the DatSource Control. For this purpose, click on the ellipse from the properties window and copy the following statement into the DELETE command text box. |
| |
| One should be careful while deleting data as it cannot be getting back once it is deleted. In order to avoid unintentional deletion, it would be better to show a message box to the end user so that they can cancel the action if they donât want to delete the item from the database. |
| |
|
| |