- Gridview Control
in .Net,it is an one of the major control. The GridView control helps you to display the number of items on a page from the Database without taking much space, with the help of paging. here, you can easily includes the paging in your application. The built-in paging concept of the Gridview is pretty good.
As soon as the number of items increases, the performance suffers. The main reason for the performance kill is that whenever you go to a new page of the GridView, it fetches all the items from the database.The GridView control helps you to selecting the item, sorting the item, and edit these items. in Gridview, every column denotes a field,every row denotes a record
- Gridview features
The GridView control supports the following features:
- Binding to data source controls, such as SqlDataSource.
- Built-in sorting
- Built-in paging
- Built-in update and delete
- Built-in row selection
- Multiple key fields.
- Multiple data fields for the hyperlink columns.
- pretty themes and styles
- Built-in row selection
- Column Fields
in Gridview,Each column can be denoted by a DataControlField object. By default, the AutoGenerateColumns property is true, than generates all the columns in the gridview underneath the data source.but if you are set the AutoGenerateColumns as false,then you want to generate columns manually.
Column Field Types
- BoundField -Displays the value of a field in a data source. This is the default column type of the GridView control.
- ButtonField -Displays a command button for each item in the GridView control. This enables you to create a column of custom button controls, such as the Add or the Remove button.
- CheckBoxField -Displays a check box for each item in the GridView control. This column field type is commonly used to display fields with a Boolean value.
- CommandField -Displays predefined command buttons to perform select, edit, or delete operations.
- HyperLinkField -Displays the value of a field in a data source as a hyperlink. This column field type enables you to bind a second field to the hyperlink's URL.
- ImageField -Displays an image for each item in the GridView control.
- TemplateField -Displays user-defined content for each item in the GridView control according to a specified template. This column field type enables you to create a custom column field.
- Binding Data to Gridview
The following 2 methods can be used to bind the GridView control to the appropriate data source type:
- For binding data source control, set the DataSourceID property of the GridView control to the ID value of the data source control.
- or programmatically set the DataSource property of the GridView control to the data source and then call the DataBind() method.
- Gridview Properties
The following properties enables you to customizing the Gridview appearance.
- AlternatingRowStyle -The style settings for the alternating data rows in the GridView control. When this property is set, the data rows are displayed alternating between the RowStyle settings and the AlternatingRowStyle settings.
- EditRowStyle -The style settings for the row being edited in the GridView control.
- EmptyDataRowStyle -The style settings for the empty data row displayed in the GridView control when the data source does not contain any records.
- FooterStyle -The style settings for the footer row of the GridView control.
- HeaderStyle -The style settings for the header row of the GridView control.
- PagerStyle -The style settings for the pager row of the GridView control.
- RowStyle -The style settings for the data rows in the GridView control.
- SelectedRowStyle -The style settings for the selected row in the GridView control.
- ShowFooter -Shows or hides the footer section of the GridView control.
- ShowHeader -Shows or hides the header section of the GridView control.
- Gridview Events
- PageIndexChanged -Occurs when one of the pager buttons is clicked, but after the GridView control handles the paging operation. This event is commonly used when you need to perform a task after the user navigates to a different page in the control.
- PageIndexChanging -Occurs when one of the pager buttons is clicked, but before the GridView control handles the paging operation. This event is often used to cancel the paging operation.
- RowCancelingEdit -Occurs when a row's Cancel button is clicked, but before the GridView control exits edit mode. This event is often used to stop the canceling operation.
- RowCommand -Occurs when a button is clicked in the GridView control. This event is often used to perform a task when a button is clicked in the control.
- RowCreated -Occurs when a new row is created in the GridView control. This event is often used to modify the contents of a row when the row is created.
- RowDataBound -Occurs when a data row is bound to data in the GridView control. This event is often used to modify the contents of a row when the row is bound to data.
- RowDeleted -Occurs when a row's Delete button is clicked, but after the GridView control deletes the record from the data source. This event is often used to check the results of the delete operation.
- RowDeleting -Occurs when a row's Delete button is clicked, but before the GridView control deletes the record from the data source. This event is often used to cancel the deleting operation.
- RowEditing -Occurs when a row's Edit button is clicked, but before the GridView control enters edit mode. This event is often used to cancel the editing operation.
- RowUpdated -Occurs when a row's Update button is clicked, but after the GridView control updates the row. This event is often used to check the results of the update operation.
- RowUpdating -Occurs when a row's Update button is clicked, but before the GridView control updates the row. This event is often used to cancel the updating operation.
- SelectedIndexChanged -Occurs when a row's Select button is clicked, but after the GridView control handles the select operation. This event is often used to perform a task after a row is selected in the control.
- SelectedIndexChanging -Occurs when a row's Select button is clicked, but before the GridView control handles the select operation. This event is often used to cancel the selection operation.
- Sorted -Occurs when the hyperlink to sort a column is clicked, but after the GridView control handles the sort operation. This event is commonly used to perform a task after the user clicks a hyperlink to sort a column.
- Sorting -Occurs when the hyperlink to sort a column is clicked, but before the GridView control handles the sort operation. This event is often used to cancel the sorting operation or to perform a custom sorting routine.
-
- Example 1
the following examples represents that assigned data source to the Gridview through SqlDataSource Control
-
<html>
<head id="Head1" runat="server">
<title>GridView Example</title>
</head>
<body>
<form id="form2" runat="server">
<h3>
GridView Example</h3>
<asp:GridView ID="CustomersGridView" DataSourceID="CustomersSource"
AutoGenerateColumns="true" EmptyDataText="No data available."
AllowPaging="true" runat="server">
</asp:GridView>
<asp:SqlDataSource ID="CustomersSource" SelectCommand="Select
[CustomerID], [CompanyName], [Address], [City], [PostalCode],
[Country] From [Customers]"
ConnectionString="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server" />
</form>
</body>
</html>
-
- Example 2
the following examples represents that assigned data source to the Gridview through Programmatically
in .aspx page
-
<asp:GridView ID="CustomersGridView" DataSourceID="CustomersSource"
AutoGenerateColumns="false" EmptyDataText="No data available." AllowPaging="true"
runat="server"> </asp:GridView>
in .aspx.cs page
-
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.
ConnectionStrings[1].ConnectionString);
string query = "select UserId,UserName from UserDetails";
SqlDataAdapter sda = new SqlDataAdapter(query, con);
DataTable dt=new DataTable();
sda.Fill(dt);
CustomersGridView.DataSource = dt;
CustomersGridView.DataBind();
}
//for Paging
protected void gvImages_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
CustomersGridView.PageIndex = e.NewPageIndex;
SqlConnection con = new SqlConnection(ConfigurationManager.
ConnectionStrings[1].ConnectionString);
string query = "select UserId,UserName from UserDetails";
SqlDataAdapter sda = new SqlDataAdapter(query, con);
DataTable dt=new DataTable();
sda.Fill(dt);
CustomersGridView.DataSource = dt;
CustomersGridView.DataBind();
}