Consider a GridView with a checkbox column. We want to select all checkboxes or clear all checkboxes within this column using a Select All checkbox in the header row.

GridView Definition

The GridView has a template column. Template defines the header row as well as the item row. In the header row, we have the Select All checkbox. And within in the item row, we have a checkbox to select an item.…

Read More

Consider a GridView that contains three columns: First Name, Alien Dropdown and Country Dropdown. The Alien Dropdown has two options, Yes and No. Select Yes in the Alien Dropdown. The Country Dropdown is enabled.

Both Dropdowns are defined in a TemplateColumn. For this example, we bind the GridView to a XmlDataSource.

<asp:Gridview ID="Grid1" runat="server" 
DataSourceID="NameXml" 
AutoGenerateColumns="false"
OnRowDataBound = "Grid1_RowDataBound" >
<Columns>
<asp:BoundField DataField="FirstName" />
<asp:TemplateField>
    <ItemTemplate>
        <asp:DropDownList ID="AlienList" runat="server" 
        AutoPostBack="true"
        OnSelectedIndexChanged="AlienList_SelectedIndexChanged" >
            <asp:ListItem Text="Yes" Value="Y"></asp:ListItem>
            <asp:ListItem Text="No" Value="N"></asp:ListItem>
        </asp:DropDownList>
    </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
    <ItemTemplate>
        <asp:DropDownList ID="CountryList" runat="server">
            <asp:ListItem Text="" Value=""></asp:ListItem>
            <asp:ListItem Text="USA" Value="USA"></asp:ListItem>
            <asp:ListItem Text="UK" Value="UK"></asp:ListItem>
        </asp:DropDownList>
    </ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:Gridview>
<asp:XmlDataSource ID="NameXml" runat="server" DataFile="Names.xml">
Read More

How do I display Country and City information like below?

  • India
    • Delhi
    • Bangalore
    • Chennai
  • USA
    • New York
    • Chicago
  • UK
    • London

For this example, we will use GridView and ObjectDataSource.

The CityHelper class fetches data from database. Fill a collection of countries. And within each country, fill a collection of cities.

// Class used by ObjectDataSource
// Fetches data from database
// Puts the data as list of countries with list of cities within
public class CityHelper
{
    private Dictionary<Country, List<City>> cities = new Dictionary<Country,List<City>>();

    public CityHelper()
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Pubs"].ConnectionString);
Read More

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

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