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:
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: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.
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.
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.