SortCommand Event handler of DataGrid not working
Posted
by Nadeem
on Stack Overflow
See other posts from Stack Overflow
or by Nadeem
Published on 2010-05-27T10:10:41Z
Indexed on
2010/05/27
11:31 UTC
Read the original article
Hit count: 257
I have created an event handler for the OnSortCommand of DataGrid:
<asp:DataGrid id="dtgBatches" runat="server" Width="100%" CssClass="intTable" EnableViewState="False" DataKeyField="bat_GUID"
GridLines="Horizontal" AllowSorting="True" AutoGenerateColumns="False" AllowPaging="False" >
<SelectedItemStyle BackColor="#FFFF99"></SelectedItemStyle>
<AlternatingItemStyle CssClass="intTableEntry"></AlternatingItemStyle>
<ItemStyle CssClass="intTableEntry2"></ItemStyle>
<HeaderStyle ForeColor="Black" CssClass="tableHeader"></HeaderStyle>
<Columns>
<asp:TemplateColumn >
<HeaderStyle Width="5%"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<img src="../images/icons/cog.png" onclick="universalAlert('Loading...',4,false); ViewBatch('<%# DataBinder.Eval(Container.DataItem, "bat_GUID") %>')" alt="view"/>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="bat_Name" SortExpression="bat_Name" HeaderText="<%$ Resources:AI360Resource, lnkbtn_Name %>">
<HeaderStyle Width="10%"></HeaderStyle>
<ItemStyle HorizontalAlign="Left"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="bat_Id" SortExpression="bat_Id" HeaderText="<%$ Resources:AI360Resource, ltxt_ID %>">
<HeaderStyle Width="10%"></HeaderStyle>
<ItemStyle HorizontalAlign="Left"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="bat_Date" SortExpression="bat_Date" HeaderText="<%$ Resources:AI360Resource, alt_date %>" DataFormatString="{0:d}">
<HeaderStyle HorizontalAlign="right" Width="10%"></HeaderStyle>
<ItemStyle HorizontalAlign="Right"></ItemStyle>
</asp:BoundColumn>
<asp:TemplateColumn>
<HeaderStyle Width="1%"></HeaderStyle>
</asp:TemplateColumn>
<asp:BoundColumn DataField="bat_Close_date" SortExpression="bat_Close_date" HeaderText="<%$ Resources:AI360Resource, ltxt_closed %>" DataFormatString="{0:d}">
<HeaderStyle Width="29%"></HeaderStyle>
<ItemStyle HorizontalAlign="Left"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="bat_Cont_Amount" SortExpression="bat_Cont_Amount" HeaderText="<%$ Resources:AI360Resource, alt_receipts %>" DataFormatString="{0:c}">
<HeaderStyle Width="10%"></HeaderStyle>
<ItemStyle HorizontalAlign="Left"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="bat_Disb_Amount" SortExpression="bat_Disb_Amount" HeaderText="<%$ Resources:AI360Resource, alt_disb %>" DataFormatString="{0:c}">
<HeaderStyle Width="25%"></HeaderStyle>
<ItemStyle HorizontalAlign="Left"></ItemStyle>
</asp:BoundColumn>
</Columns>
</asp:DataGrid>
And the code of handler is as below:
protected void dtgBatches_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
string strCurrentSort = dtgBatches.Attributes["SortExpr"];
string strNewSort = e.SortExpression;
if ((strCurrentSort != null) && (strCurrentSort == strNewSort))
{
//reverse direction
strNewSort += " DESC";
}
// Code to Set DataView dv
dv.Sort = strNewSort;
dtgBatches.DataSource = dv;
dtgBatches.DataBind();
}
The problem is that the handler never executes.
The registartion for the handler is done as below:
private void InitializeComponent()
{
this.dtgBatches.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.dtgBatches_SortCommand);
}
© Stack Overflow or respective owner