asp.netsortingdatagrid

Why does my DataGrid sorting not working with AutoGenerateColumns="False"?


The following code snippet doesn't allow sorting on a DataGrid:

<asp:DataGrid ID="tubewell" runat="server" AllowSorting="True" 
     AutoGenerateColumns="False" OnSortCommand="sortColumn">
    <Columns>
        <asp:BoundColumn DataField="tubeWell_ID" DataFormatString="#{0:c}" 
            HeaderText="Tubewell &lt;/br&gt; ID"></asp:BoundColumn>
        <asp:BoundColumn DataField="status" HeaderText="Status" Visible="False">
        </asp:BoundColumn>
    </Columns>
</asp:DataGrid>

While the following allows it:

<asp:DataGrid ID="tubewell" runat="server" AllowSorting="True" 
        AutoGenerateColumns="True" OnSortCommand="sortColumn">
</asp:DataGrid>

Why is this? How can I get sorting to work?


Solution

  • You need to set a SortExpression on the columns manually (SortExpression="myDataFieldInTheDb") when you have AutoGenerateColumns="false" set on the DataGrid