Sort Gridview ASC

Asked

Viewed 1,188 times

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>
  • You will order all the fields?

1 answer

1

The ordination of GridView has enough problems, one solution that can work is this one:

Add the sort event on 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();
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.