Tuesday, January 19, 2010

GridView "Select all” in Header

Sometimes it would be nice to have a “select all” feature for your grid check boxes. 

image

This can be easily accomplished using the header template of the grid view.

ASP.net code

<asp:GridView ID="gvTagEndorsementTypes" 
runat="server" AutoGenerateColumns="false" >
      
<Columns>                   
<asp:TemplateField HeaderText="Select">
<HeaderTemplate>
<asp:LinkButton ID="lbSelectEndorsements" 
OnClick="ToggleEndorsements_Click" runat="server"> Select 
</asp:LinkButton>
</HeaderTemplate>
<ItemTemplate >
<asp:CheckBox ID="cbSelect" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
...
C# code
protected void ToggleEndorsements_Click(object sender, EventArgs e)
{
foreach (GridViewRow erow in gvTagEndorsementTypes.Rows)
{
CheckBox echeckbox = (CheckBox)erow.FindControl("cbSelect");
//see if the checkbox is checked.             
if (echeckbox.Checked)
{ echeckbox.Checked = false; }
else { echeckbox.Checked = true; }
}
}

Last Compile Date

To check the last date of compile for a SQL procedure use this query:

select modify_date
from sys.all_objects o
where o.type = 'P'
and lower(o.name) = 'theprocname'

Tuesday, January 12, 2010

EmptyDataTemplate in a GridView

You can use the EmptyDataTemplate in your gridviews to display a message when “no data is found”.  This is an alternative to using code to check the number of rows in the gridview and show/hiding some label to indicate this.  The EmptyDataTemplate goes before the column definitions.

<asp:GridView ID="gvAuthorityExemptions"

AutoGenerateColumns="false" GridLines="None"

runat="server" Width="100%"> 
      
<EmptyDataTemplate>

<asp:Label ID="lblNoData" runat="server">

No Exemptions for License/Authority

</asp:Label>

</EmptyDataTemplate> 
      
<Columns>

Wednesday, January 6, 2010

Formatting Dates in a GridView

To display a date in mm/dd/yyyy format in a GridView use the following (make sure the “MM” is capitalized, because “mm” = minutes)

<asp:TemplateField HeaderText="Trip Date" >

<ItemTemplate >
<%# Server.HtmlEncode( Eval("TripDate", "{0:MM/dd/yyyy}").ToString() ) %>
</ItemTemplate>
</asp:TemplateField>