ASP.NET - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

ASP.NET Grid

ASP.NET Grid

shape Description

The ASP.NET Grid is used to show a collection of data of different data types. It is widely used ASP.NET control. Database connections are needed to bind the data to a grid. The basic structure of grid is [html] <asp:GridView ID="spGridView" runat="server"> </asp:GridView>[/html]

shape Conceptual figure

Below, diagram shows the basic interaction of grid.

shape Steps

One can add various types of fields to Grid in a row wise manner and can choose the format to from a set of formats. So, one have to follow below steps to add fields.

shape Step 1

Select the grid and click on ‘Edit Columns’.

shape Step 2

Now, one have to choose the fields from the ‘Available fields’ section. Then, click on ‘Add’ and then click on ‘Ok’.

shape Grid view

Now, choose a format for the grid. Let’s follow below steps for choosing .

shape Step 1

Select the grid and click on ‘Auto Format’.

shape Step 2

Now, one can choose the format from the set of existing formats. Then, click on ‘Apply’ to check the format suits our grid or not and then click on ‘Ok’. Below is a sample code for grid view. [html]<asp:GridView ID="gvPerson" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4"> <RowStyle BackColor="White" ForeColor="#003399" /> <Columns> <asp:CommandField ShowEditButton="True" /> <asp:BoundField DataField="UserName" HeaderText="UserName" ReadOnly="True" SortExpression="PersonID" /> <asp:TemplateField HeaderText="FirstName" SortExpression="FirstName"> <EditItemTemplate> <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("FirstName") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label2" runat="server" Text='<%# Bind("FirstName") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="LastName" SortExpression="LastName"> <EditItemTemplate> <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("LastName") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("LastName") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>[/html] It will appear in design like below.