Asp.Net Gridview part 1

Assalamualaykum Wr ..today we explore the asp.net’s powerful control to display data as in tabular format with built-in Edit, Update, Cancel and Insert options, named as GRIDVIEW. The general syntax of markup is as shown:
<asp:GridView ID=”gridview1″ runat=”server”>
</asp:GridView>

To add columns to the above grid, need to specify as shown:

<asp:GridView ID=”gridview1″ runat=”server”>
<Columns>
</Columns>
</asp:GridView>

Under <Columns>,specify the Columns names by using attribute as shown:
<asp:TemplateField HeaderText=”Name” SortExpressio=”Name”></asp:TemplateField>
Observe the asp:templatefield tag which has HeaderText which is your column name that appears on your gridview. SortExpression is the value based on which it sorts. If you need Three columns on Gridview to be shown then specify the three asp:templateFileds with headertext. This is all about columns, how about rows then?
We specify the rows of the gridview with label control under <ItemTemplate> which must be under <asp:TemplateField> this asp:TemplateField should be under Columns tag, as shown below:
<asp:GridView id=”gridview1″ runat=”server”>
<Columns>
<asp:TemplateField HeaderText=”Name” SortExpression=”Name”>
<ItemTemplate>
<asp:label id=”lbl_name” runat=”server” text=”Name”></label>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText=”Name” SortExpression=”Name”>
<ItemTemplate>
<asp:label id=”lbl_name” runat=”server” text=”Name”></label>
</ItemTemplate>

<asp:TemplateField HeaderText=”Qualification” SortExpression=”Qualification”>
<ItemTemplate>
<asp:label id=”lbl_Qualification” runat=”server” text=”Qualification”></label>
</ItemTemplate> <asp:TemplateField HeaderText=”Contact” SortExpression=”Contact”>
<ItemTemplate>
<asp:label id=”lbl_contact” runat=”server” text=”Contact”></label>
</ItemTemplate>
</Columns>
</asp:gridview>

This does not gives any output on ur aspx page as we are not binding data to gridview control, gridview displays data only after binding to it. In later post we would cover this topic, but in this post I just covered about the basic templates of GridView…thats all for the day..

jazakallah khair

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s