I want to illustrate how to implement nested GridViews. I will use the Authors table of Pubs database. If you recall, Pubs database is part of Microsoft SQL Server samples. We have a parent grid and a child grid. The parent grid, AuthorGrid displays last name of authors. The child grid, DetailGrid, displays more details for the selected author.

<asp:GridView ID="AuthorGrid" runat="server" DataSourceID="AuthorSql" 
                AutoGenerateColumns="false" DataKeyNames="au_lname"
                 OnSelectedIndexChanged="AuthorGrid_SelectedIndexChanged">
    <Columns>
        <asp:BoundField HeaderText="Author" DataField="au_lname" />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:GridView ID="DetailGrid" runat="server" 
                OnRowCommand="DetailGrid_OK" 
                AutoGenerateColumns="false">
                    <Columns>
                        <asp:BoundField HeaderText="First name" DataField="au_fname" />
                        <asp:ButtonField CommandName="OK" Text="OK" />
                    </Columns>
                </asp:GridView>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:CommandField ShowSelectButton="true" SelectText="Show" />
    </Columns>
</asp:GridView>
<asp:SqlDataSource ID="AuthorSql" runat="server"
                    ConnectionString = "<%$ ConnectionStrings:Pubs %>"
                   SelectCommand="SELECT au_lname FROM Authors"
></asp:SqlDataSource>
<asp:SqlDataSource ID="DetailSql" runat="server" DataSourceMode="DataSet"
                    ConnectionString = "<%$ ConnectionStrings:Pubs %>"
                   SelectCommand="SELECT au_fname FROM Authors
                         WHERE au_lname=@lname"
>
    <SelectParameters>
        <asp:Parameter Type="String" Name="lname" />
    </SelectParameters>
</asp:SqlDataSource>

AuthorGrid is bound to a data source.…

Read More

My GridView displays a list of names. On double clicking the name, the name should be set in a textbox. This should happen without any postback. We will use client side scripts for the OnDblClick event handler.

To set client side event handlers, use the RowCreated event of GridView.

<asp:TextBox ID="txtSearch" runat="server"/>
<asp:GridView ID="gvMain" runat="server" AutoGenerateColumns="false"
	 OnRowCreated="gvMain_RowCreated">
	<Columns>
	    <asp:BoundField HeaderText="Name" DataField="Name" />
        </Columns>
</asp:GridView>

RowCreated event handler sets the OnDblClick event handler.…

Read More

Repeater displays a list of items with a custom template. Our repeater shows the top 10 items. Instead of regular pagination, we have a Show More link. On clicking the link, we show all the remaining items.

We create a repeater with HeaderTemplate, ItemTemplate and FooterTemplate. HeaderTemplate begins an unordered list using the ul tag. ItemTemplate uses the li tag to show a list of items.…

Read More

GridView in ASP.NET displays tabular data. We will explore how to export this data as an excel file. Let us assume that the GridView represents a report. And we want to send the report as an email attachment. Below code exports the GridView and prepares an email attachment.

protected void Submit_Click(object sender, EventArgs e)
{
    // Render the contents of the GridView into a string
    StringBuilder sb = new StringBuilder();
    StringWriter sw = new StringWriter(sb);
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    GridView1.RenderControl(htw);
Read More