Sort GridView ASC

0

I have this grid, and I would like to sort the columns in ascending order (ASC) how to do?

<asp:GridView ID="gridView1" runat="server" AllowPaging="False" AllowSorting="True" DataSourceID="sqlTeste"
            AutoGenerateColumns="False" EmptyDataText="<%$ Resources:Mensagens, BuscaSemRegistros %>">
            <Columns>
                <asp:BoundField DataField="FUNDO" HeaderText="Fundo" SortExpression="NOME_FUNDO" />
                <asp:BoundField DataField="NUM_MES" HeaderText="Mês de Referência" SortExpression="NUM_MES_ORDENACAO" />
                <asp:BoundField DataField="COD_USUARIO" HeaderText="Usuário Responsável" SortExpression="COD_USUARIO" />
                <asp:BoundField DataField="DTA_INCLUSAO" HtmlEncode="False" DataFormatString="{0:g}"
                    HeaderText="Data/Hora Inclusão" SortExpression="DTA_INCLUSAO">
                </asp:BoundField>
            </Columns>
        </asp:GridView>
    
asked by anonymous 08.06.2015 / 15:47

1 answer

1

The ordering of GridView has a lot of problems, a solution that can work is this one:

Add the sort event in GridView

<asp:GridView ... OnSorting="gridView1_Sorting" AllowSorting="true">

And in your CodeBehind

private string ConverterSortDirection(SortDirection sortDirection)
{
    string novaDirecao = String.Empty;

    switch (sortDirection)
    {
        case SortDirection.Ascending:
            novaDirecao = "ASC";
            break;
        case SortDirection.Descending:
            novaDirecao = "DESC";
            break;
    }
    return novaDirecao;
}


protected void gridView1_Sorting(object sender, GridViewSortEventArgs e)
{
    DataTable dataTable = gridView1.DataSource as DataTable;

    if (dataTable != null)
    {
        DataView dataView = new DataView(dataTable);
        dataView.Sort = e.SortExpression + " " + ConverterSortDirection(e.SortDirection);

        gridView1.DataSource = dataView;
        gridView1.DataBind();
    }
}
    
08.06.2015 / 18:55