Onlineatoz.net | Asp.net

Bind Data into gridview in asp.net

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();
}

Posted by: Admin
Posted on: 9/14/2011 at 8:33 AM
Categories: Asp.net
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed

ToolBox

Toolbox

The common concept of the Toolbox is set of controls placed together in a box. Toolbox is one of the Window which can be easily to handle.The purpose of the Toolbox is a single click operations and in other words,Toolbox is the shortcuts of all the controls

Visual Studio .Net provides controls for two kind of applications like windows and web applications

Manage the Toolbox window

in Visual Studio,you are able to access the Toolbox as:

  • Drag the Toolbox.
  • Dock the Toolbox at the edge of a parent frame.
  • Pin a docked Toolbox open, or set it to hide itself.
The following ways helps you to manage the Toolbox:
  • To open the Toolbox -View --> Toolbox.
  • To conceal the Toolbox -Window --> Hide.
  • To Autohide the Toolbox -Window --> AutoHide.
  • To move the Toolbox -Window --> Floating and move other locations.
  • To restore all default tabs of Toolbox -on Toolbox, Rightclick --> Reset Toolbox.
Add more Items to Toolbox

in Visual Studio Toolbox,You can add items manually such as controls which can be available from your computer, a network share, or on the Web

To add items from your computer or network

  • Tools --> Toolbox Items.
  • Select Browse, now a dialog box appears
  • In the My Places Bar, select My Computer to browse for items installed on your computer drives.
    (or) In Look in, select My Network Places to browse for items located on a network share.
  • To move the Toolbox -Window --> Floating and move other locations.
  • Click OK.
ASP.NET Web Server Controls

ASP.NET Web server controls serves as ASP.NET objects of the Web pages that run when the page is requested and render to a browser. Most of the Web server controls are similar to the HTML elements, such as radiobuttons and text boxes. Moreover, Other controls such as a calendar controls and Gridview controls can manage data connections. This articls describes the available Web Server Controls and how to work with them.

Control Categories

The Web and HTML controls can be categorized as

the following screenshots represents the Visual Studio Toolbox

images/Article_Visualstudio_Toolbox.jpg
 
Creating Web Application

Dotnet enables the user who can create the Web Pages through the following controls:

  • HTML Server Control - HTML elements exposed to the server so you can program them. HTML server controls expose an object model that maps very closely to the HTML elements that they render.
  • Web server controls - Controls with more built-in features than HTML server controls. Web server controls include not only form controls such as buttons and text boxes, but also special-purpose controls such as a calendar, menus, and a tree view control. Web server controls are more abstract than HTML server controls in that their object model does not necessarily reflect HTML syntax.
  • Validation controls - Controls that incorporate logic to enable you to what users enter for input controls such as the TextBox control. Validation controls enable you to check for a required field, to test against a specific value or pattern of characters, to verify that a value lies within a range, and so on. For more information, see Validation ASP.NET Controls.
  • User controls - Controls that you create as ASP.NET Web pages. You can embed ASP.NET user controls in other ASP.NET Web pages, which is an easy way to create toolbars and other reusable elements. For more information, see ASP.NET User Controls.

Posted by: Admin
Posted on: 9/14/2011 at 8:32 AM
Categories: Asp.net
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed