What is GridView control and how to bind GridView to data?


What is a GridView control in asp.net? Want to learn how to connect GridView to a database? This article will teach you more about the databound control GridView, properties and events of GridView and binding a GridView control to data.

A GridView control is a data bound control which is used to show the data present in a data source table in a tabular manner. Like any data table Gridview is also organised in rows and columns.
A row in a gridview represents an instance or a record and each column in a row represents a field of data. Each cell displays a value which would be achieved on intersection of the respective row and column. In Asp.net, Gridview is one of the most powerful data toosl which is very useful in application development. Following are the main features supported by the GridView control:

  1. A GridView control is enriched with the following built-in features:

    1. Sorting capability: Sorting enables the users to display records in the order they want to see them.

    2. Paging capability: Paging is used by the designers to specify how many records they want the users to see at one time. Thereafter to jump to the next page some navigation tool is provided at the end.

    3. Row Selection feature

    4. Update and Deletion capabilities

  2. GridView controls have the required ability to be accessed programmatically for setting the properties and handling its events at the run time.

  3. The most important feature offered by a GridView control is that it can be bound to a datasource like SQLDataSource.

  4. The appearance of a GridView control can be customized using Themes and Styles.


The GridView control provides an option to let the programmer change the column field types. Column field types determine the behaviour of the columns. Each column in the GridView is bound to a DataControlField object. By default the columns in the control are rendered as they appear in the data source if the AutoGeneratedColumn property is set to true. However, it can be manually set to define the required set of the column fields as per the user requirements.
There are 7 types of column fields from which we can choose the type to be displayed as per the user requirement. Following are the types of Column Fields in Asp.Net:
  1. BoundField: This is the default column type which displays the type of field in the data source.

  2. CheckBoxField: This column field type is used to display check boxes in the GridView for each item/row. Learn more on How to add checkbox in a GridView

  3. ButtonField: This column field type is used to display buttons for each row in the GridView. If you want to learn more on adding buttons check out: How to add buttons in GridView

  4. CommandField: It is used to add predefined command buttons to Select, Edit and Delete items in GridView.

  5. Template Field: This column field type is used to display a custom defined column type.

  6. ImageField: This is used to display an image for each item in the GridView.

  7. HyperLinkField: This column field displays the data from the data source in the form of hyperlinks which navigates the user to the defined field/page in URL.


Adding a GridView control and binding GridView to a data source


Though there are many ways in which we can add a GridView control in a project but here I would discuss the most easiest way where the control is dragged and dropped on the VWD page and then let it guide through the set up to bind it to a data source control.

When the GridView control is on the page the smart task panel by default gets opened. Here you can apply themes, colors and style to change the appearance of the control using Auto Format. Once the control is added on the page the first thing to do is connect it to the data source. The data source control can be an SQLDataSource or an ObjectDataSource or any other collection object. One can bind the gridview either by the smart task panel as shown in the screenshot below or by specifying the data source in the DataSourceID property of the control.
Adding a GridView control in Asp.Net project

The next thing you need to do is specify what all columns you want the Data GridView control to display. Although there are many ways to edit the columns in the control, the easiest way is to use the Edit Column option given on the smart task panel. Select BoundField in the edit column fields dialog box. A list of all the columns present in the data table will be displayed from there one can add/delete the required column fields.
Editing GridView columns

Another way of editing the columns is by checking the property Auto Generate Fields given at the bottom of edit column dialog box. If this property is set to true the GridView will automatically display all the fields present in the data source table.

The last way in which the fields in the Grid View control can be edited is by switching to the source view and then editing the list of fields as shown below:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="login_ID" HeaderText="Login_ID" />

<asp:BoundField DataField="login_employee_ID" HeaderText="Employee_ID" />

<asp:BoundField DataField="Employee name" HeaderText="Employee Name" />
</Columns>
</asp:GridView>


The above mentioned method is one way to connect the data source to the GridView control another way is to programmatically bind the GridView control to the data source.


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: